代码之家  ›  专栏  ›  技术社区  ›  Schroedinger

存在于嵌套集合中找不到没有ID的?

  •  0
  • Schroedinger  · 技术社区  · 14 年前

    嘿伙计们另一个铁路问题,

    我使用的外键是item\u id,所以我尝试 invoice_items.exists?(:item_id => item.id)

    invoice_items.find(:conditions => ["item_id == ?", item.id) 我得到了一个没有invoiceItem ID就无法搜索的返回。

    思想?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Salil    14 年前
    conditions => ["item_id == ?", item.id
    

    应该是

    conditions => ["item_id = ?", item.id]
    

    invoice_items.find(:all).conditions => ["item_id = ?", item.id]
    
        2
  •  -1
  •   Geoff Lanotte    14 年前

    invoice_items.all(:conditions => ["item_id == ?", item.id])
    

    或者

    invoice_items.first(:conditions => ["item_id == ?", item.id])
    

    invoice_items.all(:conditions => {:item_id => item.id})
    

    如果你要使用 model.find :all , :first , :last 或者您正在搜索的主键。这就是为什么我通常喜欢使用 Model.find 只有在我搜索id时,否则我使用 Model.first , Model.last , Model.all