Dependant destroy with altered _id name in join table

I have a “Dependant destroy” and it wont work as its reference is not
standard . By this I mean that I have changed the XXX_id to be something
else as I had clashes: ambiguous relationships.

class Product < ActiveRecord::Base
belongs_to :company
has_many :allocations, :dependent => :destroy
has_many :companies, :through => :allocations

class Company < ActiveRecord::Base
has_many :products, :dependent => :destroy
has_many :allocations, :dependent => :destroy
has_many :allocated_products, :through => :allocations

And the joining table with an extra field
class Allocation < ActiveRecord::Base
belongs_to :company
belongs_to :allocated_product, :class_name => ‘Product’
end

I realize the error I get when testing deleting a company is due to the
fact that its looking to delete product_id from the allocations table
but cannot find it.

“Mysql::Error: Unknown column ‘allocations.product_id’ in ‘where
clause’: SELECT * FROM allocations WHERE (allocations.product_id =
3)”

I have looked for options on the call but cant see any suitable
Can I best do this by using an after delete filter, and how?

Thanks

Maybe you need to set the :foreign_key option on your association to
“some_other_id”…

On 26 January 2011 16:03, Craig L. [email protected] wrote:

I have a “Dependant destroy” and it wont work as its reference is not
standard . By this I mean that I have changed the XXX_id to be something
else as I had clashes: ambiguous relationships.

class Product < ActiveRecord::Base
belongs_to :company
has_many :allocations, :dependent => :destroy

I think you need to specify :foreign_key => ‘allocated_product_id’ on
the above.

Colin

Sir,
This Shiva,I’m a web designer
I would like to know about the Ruby on rails and would like to
learn
more bout it,can you explain bout it??&need to know the advantages of
this
concept


Thanks and Regards,
Shiva
Business Development Team,
3 DOT VENTURE
[email protected]
Phone No:+1-7024339913
Visit us at :www.3dotventure.com

Beautiful, than-you

Just so people can see solution:

class Company < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :notes, :dependent => :destroy
has_many :contacts, :through => :memberships
has_many :products, :dependent => :destroy
has_many :allocations, :dependent => :destroy, :foreign_key =>
‘allocated_product_id’
#, :dependent => :destroy
has_many :allocated_products, :through => :allocations

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