我得到了以下关联:
class User < ActiveRecord::Base
has_and_belongs_to_many :subscribers, :join_table => 'subscribers_users'
end
class Subscriber < ActiveRecord::Base
has_and_belongs_to_many :devices, :join_table => 'subscribers_devices'
end
class Device < ActiveRecord::Base
has_and_belongs_to_many :subscribers, :join_table => 'subscribers_devices'
has_many :device_attributes
end
class DeviceAttribute < ActiveRecord::Base
belongs_to :device
end
在用户类中,我的创建方法如下所示:
def self.create(params)
transaction do
# create user
user = User.new
user.assign_attributes params
# associate a device
device = Device.build_from_params(params.dup)
raise_if_invalid device
subscriber.devices.push device
# associate device_attributes to device
params[:buttons].each do |b|
# >>> problem here >>>
device.device_attributes.build({:button_id => b[:button_id]})
end
user.save
end
end
问题出现在创建设备的 device_attributes 对象的行上。注释此行时,我的用户及其关联对象(订阅者和设备)已正确创建。但是当我取消评论时,我的用户只有一个订阅者,仅此而已。它似乎停止并且不会与 devce_attributes 对象一起创建任何设备!
控制台没有错误..这对我来说真的很奇怪,怎么了?
谢谢!