# Has\_many through delete issue

**URL:** <https://www.ruby-forum.com/t/has-many-through-delete-issue/101999>\
**Category:** Rails\
**Created:** [August 9, 2006, 5:45am UTC](https://www.ruby-forum.com/t/has-many-through-delete-issue/101999 "2006-08-09T05:45:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim\_dandy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jim\_dandy](https://www.ruby-forum.com/u/jim_dandy)\
**Post date:** [August 9, 2006, 5:45am UTC](https://www.ruby-forum.com/t/has-many-through-delete-issue/101999/1 "2006-08-09T05:45:06Z")

</div>

I have the following many-to-many relationships defined in my model code  
to model a many to many relationship between Media and PriceCode with  
Price being the association table:

class Media \< ActiveRecord::Base  
has\_many :prices  
has\_many :price\_codes, :through =\> :prices  
end

class Price \< ActiveRecord::Base  
belongs\_to :media  
belongs\_to :price\_code  
end

class PriceCode \< ActiveRecord::Base  
has\_many :prices  
has\_many :medias, :through =\> :prices  
end

Based this exmaple off a recipr in the Rails Recipies Cookbook.

When I manually add data to the underlying tables, I can correctly  
display the data through by navigating through the object model.

What I’m having trouble figuring out is how to delete Price records form  
the association table.

Within one of my forms I have the following:

\<%= link\_to ‘Destroy’, { :action =\> ‘destroy\_price’, :id =\> @media.id,  
:price\_code\_id =\>price\_code.id }, :confirm =\> ‘Are you sure?’, :post =\>  
true %\>

Add here is the destroy\_price action in the controller:

def destroy\_price  
Media.find(params[:id]).prices.find(params[:price\_code\_id]).destroy  
redirect\_to :action =\> ‘show’, :id =\> params[:id]  
end

When I call the action the following error is returned:

ActiveRecord::StatementInvalid in MediaController#destroy\_price

Mysql::Error: #42S22Unknown column ‘prices.id’ in ‘where clause’: SELECT

- FROM prices WHERE (prices.id = ‘1’ AND (prices.media\_id = 82)) LIMIT  
1

For this example I’m trying to delete the prices record with media\_id of  
82 and price\_code\_id of 1. For some reason the sql that is getting  
generated by ActiveRecord is using prices.id = ‘1’ rather than  
prices.price\_code\_id = 1.

Not really sure what to do next to attempt to get this to work…

Any ideas would be greatly appreciated.

JD

---

<div class="post-metadata">

**Author:** ![jim\_dandy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jim\_dandy](https://www.ruby-forum.com/u/jim_dandy)\
**Post date:** [August 9, 2006, 6:29am UTC](https://www.ruby-forum.com/t/has-many-through-delete-issue/101999/2 "2006-08-09T06:29:56Z")

</div>

jim dandy wrote:

> I have the following many-to-many relationships defined in my model code  
> to model a many to many relationship between Media and PriceCode with  
> Price being the association table:
> 
> class Media \< ActiveRecord::Base  
> has\_many :prices  
> has\_many :price\_codes, :through =\> :prices  
> end
> 
> class Price \< ActiveRecord::Base  
> belongs\_to :media  
> belongs\_to :price\_code  
> end
> 
> class PriceCode \< ActiveRecord::Base  
> has\_many :prices  
> has\_many :medias, :through =\> :prices  
> end
> 
> Based this exmaple off a recipr in the Rails Recipies Cookbook.
> 
> When I manually add data to the underlying tables, I can correctly  
> display the data through by navigating through the object model.
> 
> What I’m having trouble figuring out is how to delete Price records form  
> the association table.
> 
> Within one of my forms I have the following:
> 
> \<%= link\_to ‘Destroy’, { :action =\> ‘destroy\_price’, :id =\> @media.id,  
> :price\_code\_id =\>price\_code.id }, :confirm =\> ‘Are you sure?’, :post =\>  
> true %\>
> 
> Add here is the destroy\_price action in the controller:
> 
> def destroy\_price  
> Media.find(params[:id]).prices.find(params[:price\_code\_id]).destroy  
> redirect\_to :action =\> ‘show’, :id =\> params[:id]  
> end
> 
> When I call the action the following error is returned:
> 
> ActiveRecord::StatementInvalid in MediaController#destroy\_price
> 
> Mysql::Error: #42S22Unknown column ‘prices.id’ in ‘where clause’: SELECT
> 
> - FROM prices WHERE (prices.id = ‘1’ AND (prices.media\_id = 82)) LIMIT  
> 1
> 
> For this example I’m trying to delete the prices record with media\_id of  
> 82 and price\_code\_id of 1. For some reason the sql that is getting  
> generated by ActiveRecord is using prices.id = ‘1’ rather than  
> prices.price\_code\_id = 1.
> 
> Not really sure what to do next to attempt to get this to work…
> 
> Any ideas would be greatly appreciated.
> 
> JD

From an iRB session I can find the Prices record, but trying to destroy  
it produces this error. I don’t know why it’s looking for an id since  
there is no id in the prices table:

> > Price.find\_by\_media\_id\_and\_price\_code\_id(82,2)  
> > =\> #\<Price:0x388a22c @attributes={“media\_id”=\>“82”,  
> > “updated\_at”=\>“2006-08-08 18:04:35”, “price\_code\_id”=\>“2”,  
> > “created\_at”=\>“2006-08-08 18:04:35”}\>

> > Price.find\_by\_media\_id\_and\_price\_code\_id(82,2).destroy  
> > ActiveRecord::StatementInvalid: Mysql::Error: Unknown column ‘id’ in  
> > ‘where clause’: DELETE FROM prices WHERE id = NULL

---

<div class="post-metadata">

**Author:** ![jim\_dandy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jim\_dandy](https://www.ruby-forum.com/u/jim_dandy)\
**Post date:** [August 9, 2006, 7:25am UTC](https://www.ruby-forum.com/t/has-many-through-delete-issue/101999/3 "2006-08-09T07:25:47Z")

</div>

jim dandy wrote:

> From an iRB session I can find the Prices record, but trying to destroy  
> it produces this error. I don’t know why it’s looking for an id since  
> there is no id in the prices table:
> 
> > > Price.find\_by\_media\_id\_and\_price\_code\_id(82,2)  
> > > =\> #\<Price:0x388a22c @attributes={“media\_id”=\>“82”,  
> > > “updated\_at”=\>“2006-08-08 18:04:35”, “price\_code\_id”=\>“2”,  
> > > “created\_at”=\>“2006-08-08 18:04:35”}\>
> 
> > > Price.find\_by\_media\_id\_and\_price\_code\_id(82,2).destroy  
> > > ActiveRecord::StatementInvalid: Mysql::Error: Unknown column ‘id’ in  
> > > ‘where clause’: DELETE FROM prices WHERE id = NULL

You NEED an id in the join model. has\_many :through won’t work without  
it.

–  
Josh S.  
[http://blog.hasmanythrough.com/](http://blog.hasmanythrough.com/)
