Ruby on Rails, find by two conditions -
i trying find values of given model combination of 2 fields.
i have following model:
create_table "users", :force => true |t| t.string "name" t.string "url" t.integer "clicks_given" t.integer "clicks_received" t.datetime "created_at" t.datetime "updated_at" end
and have defined method model:
def credits credits = self.clicks_given - self.clicks_received end
i trying find users above given credits:
@users = user.find(:all, :conditions => { "credits >= ?", -5 })
and:
@users = user.find(:all, :conditions => { "clicks_given - clicks_received >= ?", -5 })
fail. should correct statement to find right users?
pass condition array instead of hash:
@users = user.find(:all, :conditions => [ "clicks_given - clicks_received >= ?", -5 ])
the first version not work since "credits" not database column.
edit:
or in rails 3 syntax, order:
@users = user.where("clicks_given - clicks_received >= ?", 5).order("clicks_given - clicks_received")
Comments
Post a Comment