I have a model with a belongs to, something like this: model test belongs_to test2 test2 contains human readable information, there is not a foreign key between the 2 tables because the test table is auto populated with test data that might not have a human readable entry yet. So I can''t enforce a key restraint. If I do a search like: @tests = Test.find_all In the list.rhtml I have something along the lines of <table> <% for test in @tests %> <tr> <td><%=h test.field1 %></td> <td><%=h test.test2.field2 %></td> </tr> <% end %> </table> How can I check that test2 is valid before attempting to print test2.field2? test.test2_id does contain a valid value, its just that the test2 table hasn''t been updated with the new id and description yet. - Michael
Michael, you CANT reference another table without a foreign key in either of the tables. the table that has belongs_to MUST have a test2_id field and model test2 must contain has[one|many] :test in it. rails cant automagically know what test records corrispond to which test2 records. if there id <-> id then you i would say do the following: table tests ( id ... ) table test2s ( test_id .... ) model test has_[one|many] :test2[s] end model test2 set_primary_key :test_id belongs_to :test end On 8/9/05, Michael King <kingmt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a model with a belongs to, something like this: > > model test > > belongs_to test2 > > test2 contains human readable information, there is not a foreign key > between the 2 tables because the test table is auto populated with > test data that might not have a human readable entry yet. So I can''t > enforce a key restraint. > > If I do a search like: > @tests = Test.find_all > > In the list.rhtml I have something along the lines of > > <table> > <% for test in @tests %> > <tr> > <td><%=h test.field1 %></td> > <td><%=h test.test2.field2 %></td> > </tr> > <% end %> > </table> > > How can I check that test2 is valid before attempting to print > test2.field2? test.test2_id does contain a valid value, its just that > the test2 table hasn''t been updated with the new id and description > yet. > > - Michael > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
I really need to assume less when I write an email, I was try to be succinct but I think I succeeded in being cryptic. My Rails models do have the foreign key information and the relationships are defined similiar to Zachery''s definitions. My database does not have foreign keys defined. Using Zachery''s model defines for the rest of this example... If I have a test2 object and test_id is a valid number how do I verify that the test object that test2.test_id refers to really does exist before I attempt to print test2.test.field_to_be_printed? - Michael On 8/10/05, Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Michael, > > you CANT reference another table without a foreign key in either of the tables. > > the table that has belongs_to MUST have a test2_id field and model > test2 must contain has[one|many] :test in it. rails cant > automagically know what test records corrispond to which test2 > records. > > if there id <-> id then you i would say do the following: > > table tests ( > id > ... > ) > > table test2s ( > test_id > .... > ) > > model test > has_[one|many] :test2[s] > end > > model test2 > set_primary_key :test_id > belongs_to :test > end > > > On 8/9/05, Michael King <kingmt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I have a model with a belongs to, something like this: > > > > model test > > > > belongs_to test2 > > > > test2 contains human readable information, there is not a foreign key > > between the 2 tables because the test table is auto populated with > > test data that might not have a human readable entry yet. So I can''t > > enforce a key restraint. > > > > If I do a search like: > > @tests = Test.find_all > > > > In the list.rhtml I have something along the lines of > > > > <table> > > <% for test in @tests %> > > <tr> > > <td><%=h test.field1 %></td> > > <td><%=h test.test2.field2 %></td> > > </tr> > > <% end %> > > </table> > > > > How can I check that test2 is valid before attempting to print > > test2.field2? test.test2_id does contain a valid value, its just that > > the test2 table hasn''t been updated with the new id and description > > yet. > > > > - Michael > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >
Michael, you want to use the validates_associated method in your test2 object, as well as validates_presence_of to ensure you really do reference a test record to your test2. On 8/10/05, Michael King <kingmt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I really need to assume less when I write an email, I was try to be > succinct but I think I succeeded in being cryptic. My Rails models do > have the foreign key information and the relationships are defined > similiar to Zachery''s definitions. My database does not have foreign > keys defined. > > Using Zachery''s model defines for the rest of this example... If I > have a test2 object and test_id is a valid number how do I verify that > the test object that test2.test_id refers to really does exist before > I attempt to print test2.test.field_to_be_printed? > > - Michael > > On 8/10/05, Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Michael, > > > > you CANT reference another table without a foreign key in either of the tables. > > > > the table that has belongs_to MUST have a test2_id field and model > > test2 must contain has[one|many] :test in it. rails cant > > automagically know what test records corrispond to which test2 > > records. > > > > if there id <-> id then you i would say do the following: > > > > table tests ( > > id > > ... > > ) > > > > table test2s ( > > test_id > > .... > > ) > > > > model test > > has_[one|many] :test2[s] > > end > > > > model test2 > > set_primary_key :test_id > > belongs_to :test > > end > > > > > > On 8/9/05, Michael King <kingmt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have a model with a belongs to, something like this: > > > > > > model test > > > > > > belongs_to test2 > > > > > > test2 contains human readable information, there is not a foreign key > > > between the 2 tables because the test table is auto populated with > > > test data that might not have a human readable entry yet. So I can''t > > > enforce a key restraint. > > > > > > If I do a search like: > > > @tests = Test.find_all > > > > > > In the list.rhtml I have something along the lines of > > > > > > <table> > > > <% for test in @tests %> > > > <tr> > > > <td><%=h test.field1 %></td> > > > <td><%=h test.test2.field2 %></td> > > > </tr> > > > <% end %> > > > </table> > > > > > > How can I check that test2 is valid before attempting to print > > > test2.field2? test.test2_id does contain a valid value, its just that > > > the test2 table hasn''t been updated with the new id and description > > > yet. > > > > > > - Michael > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > -- > > Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>