# Model association help...

**URL:** <https://discuss.rubyonrails.org/t/model-association-help/34672>\
**Category:** rubyonrails-talk\
**Created:** [September 23, 2008, 7:00pm UTC](https://discuss.rubyonrails.org/t/model-association-help/34672 "2008-09-23T19:00:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Ressi](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@Alex\_Ressi](https://discuss.rubyonrails.org/u/Alex_Ressi)\
**Post date:** [September 23, 2008, 7:00pm UTC](https://discuss.rubyonrails.org/t/model-association-help/34672/1 "2008-09-23T19:00:55Z")

</div>

I'm having trouble with three models that need to play nice together:

- entry \<- stores diary entries - entry\_category \<- links a diary entry to a category (entry\_id, category\_id) - category \<- list of category names and attributes

I want to be able to :include =\> :categories when I perform a find, but I'm not sure if I have the model associations wrong or if my find syntax is wrong or both? An entry should only be able to be a part of one category.

Here are the models:

class EntryCategory \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :entry &nbsp;&nbsp;belongs\_to :category end

class Category \< ActiveRecord::Base &nbsp;&nbsp;has\_many :entry\_categories &nbsp;&nbsp;has\_many :entries, :through =\> :entry\_categories end

class Entry \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :user &nbsp;&nbsp;has\_one :entry\_category &nbsp;&nbsp;has\_one :category, :through =\> :entry\_category end

I want to sum the price of all entries and include the category name. I can get all the entries grouped properly like so....

@entries = Entry.sum(:price, :conditions =\> ['user\_id = 1'], :include =\> :entry\_category, :group =\> 'entry\_categories.category\_id')

...how do I also get the category.name? If I try and include categories, the query doesn't work which makes me think i have a problem with my model associations.

Any help would be greatly appreciated.

Thanks, -Alex

---

<div class="post-metadata">

**Author:** ![Andres\_Rodriguez](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@Andres\_Rodriguez](https://discuss.rubyonrails.org/u/Andres_Rodriguez)\
**Post date:** [September 23, 2008, 8:11pm UTC](https://discuss.rubyonrails.org/t/model-association-help/34672/2 "2008-09-23T20:11:15Z")

</div>

An entry can only belong to a category right? If this is the case you don’t need this many-to-many association. You just have to:

class Category \< ActiveRecord::Base

has\_many :entries end

class Entry \< ActiveRecord::Base

belongs\_to :user belongs\_to :category

end

This will let you do:

[my\_entry.category.name](http://my_entry.category.name) my\_category.entry

If you want to keep working with that model then you have an error in your query. Using :include in finders will modify the structure of your SQL query. So you have to refer to table names in conditions like this:

:conditions =\> [‘users.user\_id = 1’]

---

<div class="post-metadata">

**Author:** ![Alex\_Ressi](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@Alex\_Ressi](https://discuss.rubyonrails.org/u/Alex_Ressi)\
**Post date:** [September 23, 2008, 9:05pm UTC](https://discuss.rubyonrails.org/t/model-association-help/34672/3 "2008-09-23T21:05:48Z")

</div>

Hi Andres, thanks for the reply. I tried your suggested setup, but it didn't work. I then tried a simpler query with my existing model setup (which you see above) which did work...

@first = Entry.find(:first, :include =\> :category) @first.category.name

Both those work which is great. However, if i try my orignial query...

@entries = Entry.sum(:price, :include =\> :category, :conditions =\> ['user\_id = 1'], :group =\> 'categories.id')

I get this...

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'categories.entry\_id' in 'on clause': SELECT sum(`entries`.price) AS sum\_price, categories.id AS categories\_id FROM `entries` LEFT OUTER JOIN `categories` ON categories.entry\_id = entries.id WHERE (user\_id = 1) GROUP BY categories.id

Categories doesn't have an entry\_id, it doesn't seem to be picking up entry\_category as the join table, though based on its naming, it isn't a traditional join table otherwise it would be called category\_entry.

Any thoughts? Thanks again for your reply.

-A

---

<div class="post-metadata">

**Author:** ![Andres\_Rodriguez](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@Andres\_Rodriguez](https://discuss.rubyonrails.org/u/Andres_Rodriguez)\
**Post date:** [September 23, 2008, 9:42pm UTC](https://discuss.rubyonrails.org/t/model-association-help/34672/4 "2008-09-23T21:42:45Z")

</div>

I think it has to be with the type of relationships you set up. As I told you, you just need a one-to-many relationship reading your code. I tried your find with a many-to-many relationship and it worked for me.

---

<div class="post-metadata">

**Author:** ![Freddy\_Andersen](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddy\_Andersen](https://discuss.rubyonrails.org/u/Freddy_Andersen)\
**Post date:** [September 23, 2008, 11:10pm UTC](https://discuss.rubyonrails.org/t/model-association-help/34672/5 "2008-09-23T23:10:16Z")

</div>

> \> Categories doesn't have an entry\_id, it doesn't seem to be picking up \> entry\_category as the join table, though based on its naming, it isn't \> a traditional join table otherwise it would be called \> category\_entry.

With the setup that Andres suggested you need a entry\_id in the categories table.

---

<div class="post-metadata">

**Author:** ![Alex\_Ressi](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@Alex\_Ressi](https://discuss.rubyonrails.org/u/Alex_Ressi)\
**Post date:** [September 23, 2008, 11:44pm UTC](https://discuss.rubyonrails.org/t/model-association-help/34672/6 "2008-09-23T23:44:20Z")

</div>

Right Heimdull, thanks. So my model setup is correct for what I'm trying to achieve you think? Any idea why this works:

@first = Entry.find(:first, :include =\> :category) @first.category.name

...and this does not?

@entries = Entry.sum(:price, :include =\> :category, :conditions =\> ['user\_id = 1'], :group =\> 'categories.id')

It doesn't seem like my model setup is using the catogory\_entries table successfully as a join table. Thoughts?

-A

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 24, 2008, 9:46am UTC](https://discuss.rubyonrails.org/t/model-association-help/34672/7 "2008-09-24T09:46:45Z")

</div>

> Right Heimdull, thanks. So my model setup is correct for what I'm trying to achieve you think? Any idea why this works:
> 
> @first = Entry.find(:first, :include =\> :category) @first.category.name
> 
> ...and this does not?
> 
> @entries = Entry.sum(:price, :include =\> :category, :conditions =\> ['user\_id = 1'], :group =\> 'categories.id')

Because they are going down 2 different code paths. One is loading the  
entry and then the category. The is trying to generate the appropriate  
join statement. It just so happens that the latter is bust in rails  
2.1 for has\_one :through associations (the former is also broken but  
not in a noticeable way)

Fred

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 24, 2008, 9:56am UTC](https://discuss.rubyonrails.org/t/model-association-help/34672/8 "2008-09-24T09:56:09Z")

</div>

> > Right Heimdull, thanks. So my model setup is correct for what I'm trying to achieve you think? Any idea why this works:
> > 
> > @first = Entry.find(:first, :include =\> :category) @first.category.name
> > 
> > ...and this does not?
> > 
> > @entries = Entry.sum(:price, :include =\> :category, :conditions =\> ['user\_id = 1'], :group =\> 'categories.id')
> 
> Because they are going down 2 different code paths. One is loading  
> the entry and then the category. The is trying to generate the  
> appropriate join statement. It just so happens that the latter is  
> bust in rails 2.1 for has\_one :through associations (the former is  
> also broken but not in a noticeable way)

Forgot to add, 2.1.1 should fix this

Fred
