# Model association help

**URL:** <https://www.ruby-forum.com/t/model-association-help/149343>\
**Category:** Rails\
**Created:** [September 23, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/model-association-help/149343 "2008-09-23T21:01:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [September 23, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/model-association-help/149343/1 "2008-09-23T21:01:49Z")

</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  
belongs\_to :entry  
belongs\_to :category  
end

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

class Entry \< ActiveRecord::Base  
belongs\_to :user  
has\_one :entry\_category  
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:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [September 23, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/model-association-help/149343/2 "2008-09-23T22:11:46Z")

</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  
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’]

On Tue, Sep 23, 2008 at 2:00 PM, ressister [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> syntax is wrong or both? An entry should only be able to be a part of  
> has\_many :entry\_categories  
> I can get all the entries grouped properly like so…  
> Thanks,  
> -Alex
> 
> >

–  
ANDRES RODRIGUEZ E  
Electronic Engineer  
Software Developer  
IT Consultant

US Phone: (305) 853-8356  
Primary Mobile: +57-300-2006186  
Secondary Mobile: +57-314-7939859

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [September 23, 2008, 11:06pm UTC](https://www.ruby-forum.com/t/model-association-help/149343/3 "2008-09-23T23:06:20Z")

</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:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [September 24, 2008, 1:11am UTC](https://www.ruby-forum.com/t/model-association-help/149343/4 "2008-09-24T01:11:10Z")

</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:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [September 24, 2008, 2:19am UTC](https://www.ruby-forum.com/t/model-association-help/149343/5 "2008-09-24T02:19:39Z")

</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:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [September 23, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/model-association-help/149343/6 "2008-09-23T23:45:05Z")

</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.

On Tue, Sep 23, 2008 at 4:05 PM, ressister [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > An entry can only belong to a category right? If this is the case you  
> > end
> > 
> > > category\_id)  
> > > belongs\_to :entry  
> > > has\_one :entry\_category  
> > > categories, the query doesn’t work which makes me think i have a  
> > > Software Developer  
> > > IT Consultant
> > 
> > US Phone: (305) 853-8356  
> > Primary Mobile: +57-300-2006186  
> > Secondary Mobile: +57-314-7939859

–  
ANDRES RODRIGUEZ E  
Electronic Engineer  
Software Developer  
IT Consultant

US Phone: (305) 853-8356  
Primary Mobile: +57-300-2006186  
Secondary Mobile: +57-314-7939859

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [September 24, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/model-association-help/149343/7 "2008-09-24T12:01:16Z")

</div>

On 24 Sep 2008, at 10:46, Frederick C. wrote:

> > …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

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [September 24, 2008, 11:59am UTC](https://www.ruby-forum.com/t/model-association-help/149343/8 "2008-09-24T11:59:29Z")

</div>

On 24 Sep 2008, at 00:44, ressister wrote:

> [‘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
