Ruby 和 Rails 都是新手,但我现在受过书本教育(这显然没有任何意义,哈哈)。
我有两个模型,事件和用户通过表 EventUser 加入
class User < ActiveRecord::Base
has_many :event_users
has_many :events, :through => :event_users
end
class EventUser < ActiveRecord::Base
belongs_to :event
belongs_to :user
#For clarity's sake, EventUser also has a boolean column "active", among others
end
class Event < ActiveRecord::Base
has_many :event_users
has_many :users, :through => :event_users
end
这个项目是一个日历,我必须在其中跟踪人们注册并为给定事件刮掉他们的名字。我认为多对多是一个好方法,但我不能这样做:
u = User.find :first
active_events = u.events.find_by_active(true)
因为事件实际上没有额外的数据,所以 EventUser 模型有。虽然我可以做到:
u = User.find :first
active_events = []
u.event_users.find_by_active(true).do |eu|
active_events << eu.event
end
这似乎与“铁轨方式”相反。谁能启发我,今晚(今天早上)这一直困扰着我很长时间?