我有两个非常简单的模型,客户和约会:
class Client < ActiveRecord::Base
validates :first_name, presence: true
validates :last_name, presence: true
validates :copay, numericality: { only_integer: true }
has_many :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :clients
end
我想要做的是显示在索引页面中有约会的所有客户端。尝试通过Applications控制器执行此操作对我来说更有意义。像这样:
def index
@appointments = Appointment.client.all
end
但我无法找到正确的方法。在客户端控制器中,做这样的事情是有意义的:
@clients = Client.all(:include => :appointments)
做反向的方法是什么(即将客户拉入约会)?
答案 0 :(得分:0)
这将为所有有约会的客户提供:
@clients = Client.joins(:appointments)
我认为在你的ApplicationController
中完成这一点并不合理。由于您希望在特定页面中显示该列表,因此您应该在clients#index
或appointments#index
中执行此操作。