Topic: association renaming + through = not working?

Let say I have three tables, each having a model:

class Possession < ActiveRecord::Base
  belongs_to :product
  belongs_to :user
end

class Product < ActiveRecord::Base
  has_many :possessions
  has_many :users, :through => :possessions
end

class User < ActiveRecord::Base
  has_many :possessions
  has_many :products, :through => :possessions
end


I want to rename the array of products owned by a user to owned_products. How can I do that?

I tried:

class User < ActiveRecord::Base
  has_many :possessions
  has_many :owned_products, :class_name => "Product", :foreign_key => "product_id", :through => :possessions
end

but I get this error: ActiveRecord::HasManyThroughSourceAssociationNotFoundError

I have read the whole doc and couldnt find an example of renaming an association with the use of :through. Any suggestions?

Re: association renaming + through = not working?

Of course, while rereading the doc, I found this:

: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 its officialy impossible... so any other wayt to achieve the same result without writing a crap load of code?

Last edited by Mantat (2006-08-07 19:23:30)

Re: association renaming + through = not working?

And finaly, in the great tradition of responding to your own post, here is the solution:

has_many :owned_products, :through => :possessions, :source => :product

puting this in case someone look for similar problem using the forum search...

Re: association renaming + through = not working?

Glad to be of service wink