我有一个User
模型外键指向同一型号
class User < ActiveRecord::Base
attr_accessible :email, :name
has_many :client_workouts
end
而一个ClientWorkout
模型
class ClientWorkout < ActiveRecord::Base
attr_accessible :client_id, :trainer_id, :workout_id
belongs_to :client, :class_name => User, :foreign_key => 'client_id'
belongs_to :trainer, :class_name => User, :foreign_key => 'trainer_id'
end
我首先想知道什么,或者写的关联,当我做错事。理想情况下,我希望能够在用户的用户身份号码与client_id
或trainer_id
匹配时找到用户的客户端锻炼。所以...
user.client_workouts.trainer???
这对我来说很好。由于您的'belongs_to'关系,您应该只能调用'client_workout.client.email'或'client_workout.trainer.email'。你真的尝试过了吗? – 2013-05-09 19:25:30
是的,你说得对。我在做一些愚蠢的事情。谢谢。 – jason328 2013-05-09 19:26:30
我更新了我的问题。 – jason328 2013-05-09 19:36:43