0
我已经搜索并发现了一些与此相关的事情,但似乎无法从帖子中获得工作解决方案。Rails模型关联:两个模型之间的多个关联
我的情况:我有用户,我有Carpools;用户可能是一个拼车的司机(拼车可能只有一个驱动器),或者用户可以在拼车骑车人(拼车可能有多个车手)
这里是我的最新尝试:
class Carpool < ActiveRecord::Base
attr_accessible :rider_id
belongs_to :driver, class_name: "User", foreign_key: "driver_id"
has_many :riders, class_name: "User"
def add_rider(user)
self.riders << user
end
end
和用户
class User < ActiveRecord::Base
attr_accessible :driver_id
has_many :carpools
belongs_to :carpool, class_name: "Carpool", foreign_key: "rider_id"
end
相关模式转储:
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "name"
t.integer "driver_id"
end
create_table "carpools", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "rider_id"
end
我想我可能会执行s与外键错误...事实上,驾驶员/汽车(驾驶员has_many:carpools)和车手/ carpools(carpool has_many:车手)之间的关系是“backwords”,也正在让我的大脑雾化。
我在做什么错误什么是最好的方式来完成这件事?
任何帮助非常感谢,谢谢!