ruby on rails - Searching over multiple ActiveRecord models -
i having difficulty forming appropriate activerecord query search kitchens, have city field, , meals, have date field.
class kitchen < activerecord::base belongs_to :user has_many :meals, through: :user end class user < activerecord::base has_one :kitchen has_many :meals end class meal < activerecord::base belongs_to :user delegate :kitchen, to: :user end
i want along lines of:
date = date.new(params[:date][:year].to_i, params[:date][:month].to_i, params[:date][:day].to_i) @meals = meal.where(meal_date: date) @meals.reject { |meal| meal.kitchen.city.downcase != params[:city].downcase }
as @anthony pointed out need add together association between meal , kitchen, should able create queries against meal
based on city of kitchen
.
class kitchen < activerecord::base belongs_to :user has_many :meals end class user < activerecord::base has_one :kitchen has_many :meals end class meal < activerecord::base belongs_to :user belongs_to :kitchen end
for example, like:
meal.joins(:kitchen).where(meal_date: date, kitchen: {city != params[:city]})
see conditions on joined tables more information.
ruby-on-rails ruby activerecord
No comments:
Post a Comment