Rails 3: Create an "empty" ActiveRecord query object -
is possible in rails, create "empty" activerecord query object given model? empty means me condition, includes possible rows in db. want apply filters query afterwards.
at moment using booking.where('id > ?', 0)
, believe there has nicer way booking.all()
- all()
returns array , not activerecord. here´s code:
@bookings = booking.where('id > ?', 0) if !@day.nil? @bookings = @bookings.where('day = ?',@day) end if @project_id && !@project_id.empty? @bookings = @bookings.where('project_id = ?', @project_id) end
thanks help!
you can use scoped method discussed here: http://guides.rubyonrails.org/active_record_querying.html#working-with-scopes
also note @project_id.present?
same @project_id && !@project_id.empty?
Comments
Post a Comment