我想ActiveRecord
通过表中的非 id 列进行查找。希望当我给你我的代码示例时这一点很清楚。
class CoachClass < ActiveRecord::Base
belongs_to :coach
end
class Coach < ActiveRecord::Base
has_many :coach_classes, :foreign_key => 'user_name'
end
当我做 a
coach_obj.coach_classes
时,这会正确触发
SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 2)
(2 是那个教练在id
这里,这是我的问题。)
我想让它触发
SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 'David')
(“大卫”是那个教练的user_name
)
user_name
是独一无二的,并且出现在两个表中。
出于某种原因,我不想coach_id
在我的桌子上放一个。coach_classes