我有两个表事件和作业
events assignments
===== ============
sport_id sport_id
home_school_id school_id
name division_id
我正在尝试选择事件并像这样加入任务:
joins = "LEFT JOIN assignments sa on events.sport_id = sa.sport_id
AND events.home_school_id = sa.school_id"
events = Event.find(:all, :joins => joins)
问题是这不会返回除法_id。为什么不 ?是因为没有协会吗?
我不应该做events.first.division_id
吗?我已经在 mysql 中尝试过,它工作正常。
编辑
我的事件模型关联
has_many :assignments,
:primary_key => :sport_id,
:foreign_key => :sport_id,
:finder_sql =>
'SELECT sa.* ' +
'FROM events e, assignments sa ' +
'WHERE e.sport_id = sa.sport_id AND e.home_school_id = sa.school_id ' +
'ORDER BY e.event_date'