I am trying to find all values of a given model by a combination of two fields.
I have the following model:
create_table "users", :force => true do |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 I have defined this method for the model:
def credits
credits = self.clicks_given - self.clicks_received
end
I am trying to find all users above a given credits:
@users = User.find(:all, :conditions => { "credits >= ?", -5 })
and:
@users = User.find(:all, :conditions => { "clicks_given - clicks_received >= ?", -5 })
Fail. What should is the correct statement to to find the right users?
Pass the condition as an array instead of a hash:
The first version will not work since “credits” is not a database column.
EDIT:
Or in Rails 3 syntax, with your order: