Hello everyone. I trying to learn associations, but have a quick question that shouldn''t be too hard. I have a classifieds table that has a column category_id and I have a categories table. I put has_many :classifieds in the Category model and belongs_to :categories in the Classifieds model I am trying to get the category for a classified from the view. What is the correct way to do this? I thought it would be something like: <% for classified in @classifieds %> <%= classified.category.name %> <% end %> but obviously it isn''t working. What is the right way to do this? Thanks! -Bradly --~--~---------~--~----~------------~-------~--~----~ 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 think it should be belongs_to :category in your Classified model. -- Scott Becker Electro Interactive, Inc. Office: 813-333-5508 http://www.ElectroInteractive.com On 11/13/06, Bradly <bradlyf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Hello everyone. I trying to learn associations, but have a quick > question that shouldn''t be too hard. > > I have a classifieds table that has a column category_id and I have a > categories table. I put has_many :classifieds in the Category model and > belongs_to :categories in the Classifieds model > > I am trying to get the category for a classified from the view. What is > the correct way to do this? I thought it would be something like: > > <% for classified in @classifieds %> > <%= classified.category.name %> > <% end %> > > but obviously it isn''t working. What is the right way to do this? > Thanks! > > -Bradly > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yes, the "belongs_to" part should be singular while the "has_many" part should be plural. Check this for a good example: http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=5 Cheers, Daniel. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---