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
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 @account
Thorsten M. 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 @account
That was it! I had @account = params[:id] instead of @account =
Account.find_by_id(params[:id]). Thanks for the help,
-S