sql - rails - find with condition in rails 4 -
i upgraded rails rails 4.1.6.
this query used work :
@user = user.find(:all, :conditions => { :name => 'batman' })
now error message:
couldn't find users 'id': (all, {:conditions=>{:name=>"batman"}}) (found 0 results, looking 2)
when check logs can see rails trying different query :
user load (0.4ms) select "users".* "users" "users"."id" in ('all', '--- :conditions: :name: batman ')
it looks like, it's trying users id "all" , "{:conditions=>{:name=>"batman"}}". please help.
update:
my real question behind want specific user , add together cars, cars blue. illustration query, user id 20.
@user = user.joins(:cars).find(20, :cars => {:color => "blue"})
but error:
couldn't find users 'id': (20, {:cars=>{:color=>"blue"}}) (found 41 results, looking 2)
some others pointed out: query syntax changed. seek this:
@user = user.joins(:cars).where(:cars => { :color => "blue" }).find(20)
note raise exception if record not found, homecoming array empty instead call:
@user = user.joins(:cars).where(:id => 20, :cars => { :color => "blue" })
i suggest read: http://guides.rubyonrails.org/active_record_querying.html
if want load user if not have cars , display bluish cars, this:
@user = user.find(20) # returns user @user.cars.where(:color => 'blue') # returns user's bluish cars (or empty array)
sql ruby-on-rails ruby find condition
No comments:
Post a Comment