0
我很难形成适当的ActiveRecord查询来搜索具有城市字段的厨房和有日期字段的餐食。搜索多个ActiveRecord模型
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
我想做的事的东西线:
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 }
你好伙计启动器!餐和厨房没有任何关联,所以'meal.kitchen'会炸毁。那里有关系吗? – Anthony 2014-10-17 16:01:40
嘿,朋友!你从哪个队列毕业?我推出了2013年秋季。 – Richard 2014-10-17 17:41:03
我更新了我的代码。我错过了协会。 – Richard 2014-10-17 17:51:08