# Rails associations question

**URL:** <https://www.ruby-forum.com/t/rails-associations-question/128437>\
**Category:** Rails\
**Created:** [February 7, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/rails-associations-question/128437 "2008-02-07T18:05:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thoughtware.TV](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ac8455/32.png) [@Thoughtware.TV](https://www.ruby-forum.com/u/Thoughtware.TV)\
**Post date:** [February 7, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/rails-associations-question/128437/1 "2008-02-07T18:05:18Z")

</div>

I have two models:

Post:  
has\_many :votes

Vote:  
belongs\_to :post

when i do:

v = Vote.find(1)

I can access all the post information.

If I get a post, howerver:

p = Post.find(1)

And try to get the votes:

total = p.votes.count

I get an undefined method error.

The foreign keys are correct, and I can’t seem to figure this out.

---

<div class="post-metadata">

**Author:** ![Thoughtware.TV](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ac8455/32.png) [@Thoughtware.TV](https://www.ruby-forum.com/u/Thoughtware.TV)\
**Post date:** [February 7, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/rails-associations-question/128437/2 "2008-02-07T18:33:11Z")

</div>

Basically what I need is to be able to do:

> > p.votes.count

But instead of an amount, I get this:

NoMethodError: undefined method `votes' for #<Post:0x4b2e7c0> from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/ active_recor d/base.rb:1792:in`method\_missing’

I can do this:

> > v.post.title  
> > =\> “This is the post title.”

So as you can see, I am able to access the post information by  
accessing a particular vote, but I can’t get all votes for a single  
post.

---

<div class="post-metadata">

**Author:** ![Thoughtware.TV](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ac8455/32.png) [@Thoughtware.TV](https://www.ruby-forum.com/u/Thoughtware.TV)\
**Post date:** [February 7, 2008, 7:52pm UTC](https://www.ruby-forum.com/t/rails-associations-question/128437/3 "2008-02-07T19:52:25Z")

</div>

[Thoughtware.tv](http://Thoughtware.tv) wrote:

> Basically what I need is to be able to do:
> 
> > > p.votes.count
> 
> But instead of an amount, I get this:
> 
> NoMethodError: undefined method `votes' for #<Post:0x4b2e7c0> from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/ active_recor d/base.rb:1792:in `method\_missing’
> 
> I can do this:
> 
> > > v.post.title  
> > > =\> “This is the post title.”
> 
> So as you can see, I am able to access the post information by  
> accessing a particular vote, but I can’t get all votes for a single  
> post.

In your Post class you need to look carfuly at your ‘has\_many :votes’  
statement. Does it exist? Is ‘:votes’ spelled correctly and is it a  
symbol or string?

The error message you are posting has nothing to do with the database  
setup, it is is saying that the Model you are using is not set up the  
way you think. If this doesn’t solve your problem, it would be helpful  
to see the relevant code from your Post class.

---

<div class="post-metadata">

**Author:** ![Thoughtware.TV](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ac8455/32.png) [@Thoughtware.TV](https://www.ruby-forum.com/u/Thoughtware.TV)\
**Post date:** [February 10, 2008, 12:32am UTC](https://www.ruby-forum.com/t/rails-associations-question/128437/4 "2008-02-10T00:32:16Z")

</div>

Its a symbol and it has\_many :votes as I sated above, I checked once  
again.

I will post the relevant code.

On Feb 7, 2:52 pm, Starr T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
