false" />
开发者

rails active record - complicated conditions clause

开发者 https://www.devze.com 2022-12-16 02:33 出处:网络
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

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

精彩评论

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

关注公众号