# One to many asscoaiation

**URL:** <https://www.ruby-forum.com/t/one-to-many-asscoaiation/139755>\
**Category:** Rails\
**Created:** [June 6, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/one-to-many-asscoaiation/139755 "2008-06-06T18:41:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [June 6, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/one-to-many-asscoaiation/139755/1 "2008-06-06T18:41:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [June 6, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/one-to-many-asscoaiation/139755/2 "2008-06-06T19:00:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [June 6, 2008, 7:22pm UTC](https://www.ruby-forum.com/t/one-to-many-asscoaiation/139755/3 "2008-06-06T19:22:53Z")

</div>

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
