New to Ruby on Rails so please be patient. I have two tables policies and standards Policies has id, and PolicyName fields Standards has id, StandardName and ParentPolicyId fields. Each tale has a primary key on id and Standards has a foreign key on ParentPolicyId referencing Policies.id. My model is as follws:- class Standard < ActiveRecord::Base belongs_to :policy end class Policies < ActiveRecord::Base has_many :standards end I am simply trying to list policies and all their related standards using the following controller code. def listrelations2 @policies = Policy.find_all end ...and the following rhtml code:- <% @policies.each do |policy| %> <tr> <td>= policy.id %></td> <td>= policy.PolicyName %></td> <td>= policy.standard.StandardName %></td> </tr> <% end %> I just cant understand the rhtml code that I should use to reference a field in standards via its foreign key relationsip to policies. I thought that the foreign key would set up the required links such that I can just refer to the field in the standards table via the policy.standards.StandardName syntax. However I get errors about .standards not being a method of polcies. I appreciate this is probably a ''day one'' misunderstanding but help out an old coder trying new things!.....What should the rhtml code be or is my error elsewhere? Thanks in advance for any help. Martyn -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok, your error lies in the fact that you''re not using the magic names for your fields, which can be solved two ways: #1 Use the magic names -> change ParentPolicyId to policy_id #2 Use the code that lets you specify your own: in Standard: belongs_to :policy, :foreign_key => ''ParentPolicyId'' in Policy: has_many :standards, :foreign_key => ''ParentPolicyId'' One more point of interest to you: In your rhtml, you''re doing policy.standard.StandardName... since a Policy has_many Standards, you can''t do that. You would need something like the following: <% Policy.standards.each do |standard| %> <%= standard.StandardName %> <% end %> That loops through all the standards, and displays the name of each one. On 2/27/07, Martyn Elmy-liddiard <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > New to Ruby on Rails so please be patient. > > I have two tables policies and standards > > Policies has id, and PolicyName fields > Standards has id, StandardName and ParentPolicyId fields. > > Each tale has a primary key on id and Standards has a foreign key on > ParentPolicyId referencing Policies.id. > > My model is as follws:- > > class Standard < ActiveRecord::Base > belongs_to :policy > end > > class Policies < ActiveRecord::Base > has_many :standards > end > > I am simply trying to list policies and all their related standards > using the following controller code. > > def listrelations2 > @policies = Policy.find_all > end > > ...and the following rhtml code:- > > <% @policies.each do |policy| %> > <tr> > <td>= policy.id %></td> > <td>= policy.PolicyName %></td> > <td>= policy.standard.StandardName %></td> > </tr> > <% end %> > > I just cant understand the rhtml code that I should use to reference a > field in standards via its foreign key relationsip to policies. I > thought that the foreign key would set up the required links such that I > can just refer to the field in the standards table via the > policy.standards.StandardName syntax. However I get errors about > .standards not being a method of polcies. > > I appreciate this is probably a ''day one'' misunderstanding but help out > an old coder trying new things!.....What should the rhtml code be or is > my error elsewhere? > > Thanks in advance for any help. > > Martyn > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Luke Ivers wrote:> Ok, your error lies in the fact that you''re not using the magic names > for > your fields, which can be solved two ways: > #1 Use the magic names -> change ParentPolicyId to policy_id > #2 Use the code that lets you specify your own: > in Standard: > belongs_to :policy, :foreign_key => ''ParentPolicyId'' > in Policy: > has_many :standards, :foreign_key => ''ParentPolicyId'' > > One more point of interest to you: > In your rhtml, you''re doing policy.standard.StandardName... since a > Policy > has_many Standards, you can''t do that. > You would need something like the following: > <% Policy.standards.each do |standard| %> > <%= standard.StandardName %> > <% end %> > That loops through all the standards, and displays the name of each one.Luke, thanks for the prompt reply. My policies.rb now look like:- class Policies < ActiveRecord::Base has_many :standards, :foreign_key => ''ParentPolicyId'' end and my standard.rb look like:- class Standard < ActiveRecord::Base belongs_to :policy, :foreign_key => ''ParentPolicyId'' end and my rhtml now looks like this:- <% @policies.each do |policy| %> <tr> <td><font size="1" face="arial"><%= policy.id %></td> <td><font size="1" face="arial"><%= policy.PolicyName %></td> <td><% Policy.standards.each do |standard| %> <%= standard.StandardsName %></td> <% end %> </tr> <% end %> ....but I still get the following error:- undefined method `standards'' for Policy:Class -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m sorry, I mis-capitalized and it screwed everything up. That should be policy.standards.each do |standard| instead of Policy.standards.each do |standard| One references the class, the other references the instance. On 2/27/07, Martyn Elmy-liddiard <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Luke Ivers wrote: > > Ok, your error lies in the fact that you''re not using the magic names > > for > > your fields, which can be solved two ways: > > #1 Use the magic names -> change ParentPolicyId to policy_id > > #2 Use the code that lets you specify your own: > > in Standard: > > belongs_to :policy, :foreign_key => ''ParentPolicyId'' > > in Policy: > > has_many :standards, :foreign_key => ''ParentPolicyId'' > > > > One more point of interest to you: > > In your rhtml, you''re doing policy.standard.StandardName... since a > > Policy > > has_many Standards, you can''t do that. > > You would need something like the following: > > <% Policy.standards.each do |standard| %> > > <%= standard.StandardName %> > > <% end %> > > That loops through all the standards, and displays the name of each one. > > Luke, thanks for the prompt reply. > > My policies.rb now look like:- > > class Policies < ActiveRecord::Base > has_many :standards, :foreign_key => ''ParentPolicyId'' > end > > and my standard.rb look like:- > > class Standard < ActiveRecord::Base > belongs_to :policy, :foreign_key => ''ParentPolicyId'' > end > > and my rhtml now looks like this:- > <% @policies.each do |policy| %> > > <tr> > <td><font size="1" face="arial"><%= policy.id %></td> > <td><font size="1" face="arial"><%= policy.PolicyName %></td> > <td><% Policy.standards.each do |standard| %> > <%= standard.StandardsName %></td> > <% end %> > > </tr> > <% end %> > > ....but I still get the following error:- > undefined method `standards'' for Policy:Class > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Luke Ivers wrote:> I''m sorry, I mis-capitalized and it screwed everything up. > > That should be policy.standards.each do |standard| instead of > Policy.standards.each do |standard| > One references the class, the other references the instance.Luke, My rhtml now looks like:- <% @policies.each do |policy| %> <tr> <td><%= policy.id %></td> <td><%= policy.PolicyName %></td> <td><% policy.standards.each do |standard| %> <%= standard.StandardsName %></td> <% end %> </tr> <% end %> ..but I still get:- undefined method `standards'' for #<Policy:0x90524a0> Is there something else I could be missing here? Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hmm, not entirely sure what problem you''re seeing, but something I did notice that is incorrect (and probably again my fault): belongs_to policy, :foreign_key => ''ParentPolicyId'' should probably be belongs_to :policy, :class_name => ''Policies'', :foreign_key => ''ParentPolicyId'' You may want to consider having your table named policies and your model named Policy... that would be the standard for Rails, and might make things work more easily. On 2/27/07, Martyn Elmy-liddiard <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Luke Ivers wrote: > > I''m sorry, I mis-capitalized and it screwed everything up. > > > > That should be policy.standards.each do |standard| instead of > > Policy.standards.each do |standard| > > One references the class, the other references the instance. > > > Luke, > > My rhtml now looks like:- > > <% @policies.each do |policy| %> > <tr> > <td><%= policy.id %></td> > <td><%= policy.PolicyName %></td> > <td><% policy.standards.each do |standard| %> > <%= standard.StandardsName %></td> > <% end %> > </tr> > <% end %> > > ..but I still get:- > > undefined method `standards'' for #<Policy:0x90524a0> > > Is there something else I could be missing here? > > Thanks > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Luke Ivers wrote:> Hmm, not entirely sure what problem you''re seeing, but something I did > notice that is incorrect (and probably again my fault): > belongs_to policy, :foreign_key => ''ParentPolicyId'' > should probably be > belongs_to :policy, :class_name => ''Policies'', :foreign_key => > ''ParentPolicyId'' > > You may want to consider having your table named policies and your model > named Policy... that would be the standard for Rails, and might make > things > work more easily.Thanks Luke. I''ll think I''ll take your advice and do things properly as per Rails standards. Hopefully I can then get somewhere. Dont want to give up! Regards Martyn -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Martyn Elmy-liddiard wrote:> Luke Ivers wrote: >> Hmm, not entirely sure what problem you''re seeing, but something I did >> notice that is incorrect (and probably again my fault): >> belongs_to policy, :foreign_key => ''ParentPolicyId'' >> should probably be >> belongs_to :policy, :class_name => ''Policies'', :foreign_key => >> ''ParentPolicyId'' >> >> You may want to consider having your table named policies and your model >> named Policy... that would be the standard for Rails, and might make >> things >> work more easily. > > Thanks Luke. > > I''ll think I''ll take your advice and do things properly as per Rails > standards. > Hopefully I can then get somewhere. Dont want to give up! > > Regards > > MartynLuke: Having repaired the basics i.e. recreating the model and controller as Policy and not Policies it now works! Thanks for your assistance....Appreciated! Martyn -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---