Has_many :through a belongs_to

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

On 8/17/07, Pat M. [email protected] 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://weblog.techno-weenie.net
http://mephistoblog.com

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.” [email protected] wrote:

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

tions for :class_name and :foreign_key are ignored, as the

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs