Ruby on rails where() with a method -


i want perform where() query, on method of model. how this?

if can't, there way can this:

session.find_each.select |session|   session.end_date > params[:date_from].to_date && session.end_date < params[:date_to].to_date end 

but returning activerecordrelation instead of array?

session.rb

class session < activerecord::base   belongs_to :course   delegate :units, to: :course    def end_date     start_date + units.count.weeks   end end 

end_date method, start_date field.

you can, you'll have use joins

units belongs course, you'll have join courses table before applying where. otherwise, fetch sessions, , filter 1 one based on end_date, generate lots sql queries.

session.joins(:course).where("calculate_end_date() > ? , calculated_end_date() < ?", date, date) 

you'll need calculate end date use sql functions, google


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -