Ruby和Rails都是新手,但我现在已经预订了教育(显然没什么意义,哈哈).
我有两个模型,Event和User通过表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
Run Code Online (Sandbox Code Playgroud)
这个项目是一个日历,我必须跟踪人们注册并为特定事件划出他们的名字.我认为多对多是一个很好的方法,但我做不到这样的事情:
u = User.find :first
active_events = u.events.find_by_active(true)
Run Code Online (Sandbox Code Playgroud)
因为事件实际上没有那些额外的数据,所以EventUser模型可以.虽然我能做到:
u = User.find :first
active_events = []
u.event_users.find_by_active(true).do |eu|
active_events << eu.event
end
Run Code Online (Sandbox Code Playgroud)
这似乎与"铁路方式"相反.任何人都可以启发我,今晚(今天早上)这已经困扰了我很长时间?