你好,我有两个模型,它们之间有has_one的关联。我需要使用委托字段进行搜索。
**Model 1:**
class Reservation < ActiveRecord::Base
belongs_to :content
delegate :type, :title, :to => :content
end
**Model 2:**
class Content < ActiveRecord::Base
has_one :reservation
end
以下查询可以正常工作,因为使用了委托(delegate):
reservations = Reservation.last
reservations.title
~ Content Load (0.6ms) SELECT `contents`.* FROM `contents` WHERE `contents`.`id` = 95 LIMIT 1
=> "Birthday Party"
现在我需要使用委托字段进行查询:
reservations = Reservation.where("title = ?","some_title")
它返回错误:
Unknown column 'title' in 'where clause'
我该如何解决问题?我的方法正确吗?谢谢阅读我的问题。