我需要一些帮助,我的Rails 4协会。我有以下4种型号:
class User < ActiveRecord::Base
has_many :check_ins
has_many :weigh_ins, :through => :check_ins
has_many :repositionings, :through => :check_ins
end
class CheckIn < ActiveRecord::Base
belongs_to :user
has_one :weigh_in
has_one :repositioning
end
class Repositioning < ActiveRecord::Base
# belongs_to :user
belongs_to :check_in
end
class WeighIn < ActiveRecord::Base
# belongs_to :user
belongs_to :check_in
end问题:如果我是这样设置的话,我如何分别输入repositionings和weigh_ins,但仍然通过一次签入将它们链接起来?
发布于 2016-05-02 14:07:41
为了使其工作,您必须保留另一个协会的ID。
例如,假设:
correct_checkin_record = CheckIn.where(repositioning: the_repositioning_id)另一种(也更简单)的方法是通过用户直接访问CheckIn:correct_checkin_record = @user.checkin --这每次都会引入正确的CheckIn。
我包括了两个选项,以帮助形象化在关系中到底发生了什么。
发布于 2016-05-02 14:07:33
您想让用户在不同的页面上输入weigh_ins和职位吗?
将weigh_ins和repositionings分开输入,但仍然是单个签入的一部分,这样的设置是可以的。
这只是获取同一个check_in对象并使关联与该对象相关联的问题,这可以通过通过传入check_in ID参数和执行CheckIn.find(params[:id])来完成。
https://stackoverflow.com/questions/36984199
复制相似问题