Hey guys another rails issue,
Cur开发者_开发问答rently have a collection that is line items for an invoicing system. I want to increment the count of the line items if I add in an item that already exists. At the moment I'm using an exists? query on the collection but it seems to return regardless of the key.
The foreign key I'm using is item_id, so I try to do invoice_items.exists?(:item_id => item.id)
This wasn't returning, so I changed it to invoice_items.find(:conditions => ["item_id == ?", item.id)
and I get a return that I cannot search without invoiceItem ID.
Ideas?
conditions => ["item_id == ?", item.id
should be
conditions => ["item_id = ?", item.id]
So your query look like this
invoice_items.find(:all).conditions => ["item_id = ?", item.id]
you should just need to do either
invoice_items.all(:conditions => ["item_id == ?", item.id])
OR
invoice_items.first(:conditions => ["item_id == ?", item.id])
and you can use the syntax
invoice_items.all(:conditions => {:item_id => item.id})
if you are going to use the model.find
command the first parameter needs to be :all
, :first
, :last
or the primary key you are searching for. that is why I generally prefer to use Model.find
only if I am searching for an id, otherwise I use Model.first
, Model.last
, Model.all
. That way you know what you are going to get.
精彩评论