I have an account and an account can have many creditcards, for example. So I have my models set as: class Account < ActiveRecord::Base has_many :creditcards end and class Creditcards < ActiveRecord::Base belongs_to :account end Now, in an .rhtml file I am trying to say something like this: <% if @account.creditcards.count > 0 %> Code to display all creditcards asscociated with this account <% end %> Instead what I see is this error: undefined method `creditcards'' for "156429":String I keep on looking at my code and I can''t see anything wrong. Isn''t this the correct way to set up a one-to-many relationship? Thanks, -S -- 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 -~----------~----~----~----~------~----~------~--~---
Your setup seems to be ok. But the error:> undefined method `creditcards'' for "156429":Stringsays that @account is a string containing "156429". (maybe the id of the record) Anyway, the error is most likely in your find, please post the code that assigns @account --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller wrote:> Your setup seems to be ok. > > But the error: >> undefined method `creditcards'' for "156429":String > says that @account is a string containing "156429". (maybe the id of > the record) > Anyway, the error is most likely in your find, please post the code > that assigns @accountThat was it! I had @account = params[:id] instead of @account = Account.find_by_id(params[:id]). Thanks for the help, -S -- 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 -~----------~----~----~----~------~----~------~--~---