# Has\_many :through a belongs\_to

**URL:** <https://www.ruby-forum.com/t/has-many-through-a-belongs-to/112370>\
**Category:** Rails\
**Created:** [August 18, 2007, 12:20am UTC](https://www.ruby-forum.com/t/has-many-through-a-belongs-to/112370 "2007-08-18T00:20:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 18, 2007, 12:20am UTC](https://www.ruby-forum.com/t/has-many-through-a-belongs-to/112370/1 "2007-08-18T00:20:51Z")

</div>

I’m trying to set up a has\_many through association, and it should go  
through a belongs\_to assocation.

class Company  
has\_many :videos  
end

class Site  
belongs\_to :company  
has\_many :videos, :through =\> :company  
end

Site.find(:first).videos

ActiveRecord::StatementInvalid: PGError: ERROR: column  
companies.company\_id does not exist  
: SELECT videos.\* FROM videos INNER JOIN companies ON  
videos.company\_id = companies.id WHERE ((companies.company\_id = 1))

From the documentation:

“:through: Specifies a Join Model to perform the query through.  
Options for :class\_name and :foreign\_key are ignored, as the  
association uses the source reflection. You can only use a :through  
query through a belongs\_to or has\_many association.”

So it seems as though I should be able to do it…but AR is doing the  
join incorrectly. Any ideas?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 18, 2007, 1:35am UTC](https://www.ruby-forum.com/t/has-many-through-a-belongs-to/112370/2 "2007-08-18T01:35:59Z")

</div>

On 8/17/07, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :videos, :through =\> :company
> 
> “:through: Specifies a Join Model to perform the query through.  
> Options for :class\_name and :foreign\_key are ignored, as the  
> association uses the source reflection. You can only use a :through  
> query through a belongs\_to or has\_many association.”
> 
> So it seems as though I should be able to do it…but AR is doing the  
> join incorrectly. Any ideas?

site.company.videos ?

or…

class Site  
belongs\_to :company  
delegate :videos, :to =\> :company  
end

Perhaps the docs are unclear? Here’s a couple has\_many :through  
examples:

class Article  
has\_many :comments  
has\_many :users, :through =\> comments  
end

class Comment  
belongs\_to :user  
belongs\_to :article  
end

class User  
has\_many :articles  
has\_many :comments, :through =\> :articles  
end

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [August 18, 2007, 6:21am UTC](https://www.ruby-forum.com/t/has-many-through-a-belongs-to/112370/3 "2007-08-18T06:21:18Z")

</div>

I’m a newbie, but I’m going to take a crack at this one.

> From what I understand, you have three models: Site, Company, and  
> Video

> From your example, I don’t understand which model describes the pivot  
> table – the table in the middle of the many to many relationship I  
> believe you are trying to establish, if I understand correctly. I  
> think your pivot table is sites, for the model Site. I believe that  
> would look like this:

class Company  
has\_many :sites  
has\_many :videos :through =\> :sites  
end

class Site  
belongs\_to :companies  
belongs\_to :videos  
end

class Video  
has\_many :sites  
has\_many :companies :through =\> :sites  
end

If you are trying to make a many to many relationship between videos  
and companies, then the above is how you do it. The pivot table  
belongs to each of the tables it joins; each table in the many-to-many  
relationship has\_many of the other table, but each also has\_many of  
the pivot table.

Charlie

On Aug 17, 3:20 pm, “Pat M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> ":through: Specifies a Join Model to perform the query through.  
> O

tions for :class\_name and :foreign\_key are ignored, as the
