我是 Rails 新手,所以请多多包涵……</p>
我正在尝试创建一组与 2 个不同模型相关的数据。我目前有以下型号:
class M < ActiveRecord::Base
belongs_to :u
belongs_to :s
end
class U < ActiveRecord::Base
has_many :m
has_many :s, :through => m:
end
class S < ActiveRecord::Base
has_many :m
has_many :u, :through => m;
end
在系统中,用户可以创建多个Us和Ss。但是在创建 M 时,应确保存在对“u”和“s”的引用。
我知道我可以做到以下几点:
m1 = M.create()
u1.ms << m1
s1.ms << m1
哪个有所有适当的参考,有没有更好的方法?