false" />
开发者

rails active record - complicated conditions clause

this works:

ids = [1,2]
varietals = Varietal.find(:all, :conditions => [ "id IN (?)",ids])

But what I want to do is that plus have a condition of: deleted => false

varietals = Varietal.find(:all, :co开发者_C百科nditions =>{ :deleted => false})

any ideas?

am i going to have to use find_by_sql?


I would handle this with a named_scope to communicate intent and foster re-use:

named_scope :undeleted,
            :conditions => { :deleted => false }

Then you can simply use:

varietals = Varietal.undeleted.find([1,2])


You can do it a few ways, but this is the most straight forward:

varietals = Varietal.find( [1,2], :conditions => { :deleted => false })

You can see in the docs that the first parameter of find can take an integer or an array.


ids = [1,2]
varietals = Varietal.find(:all, :conditions => {:id => ids, :deleted => false})

This should work, haven't tested it though.

From the docs:

An array may be used in the hash to use the SQL IN operator:

Student.find(:all, :conditions => { :grade => [9,11,12] })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜