我有一个User类:
class User < ActiveRecord::Base
has_many :devices, dependent: :destroy
has_many :push_registrations, dependent: :destroy
end
push_registrations
表上有一个foreign_key约束:
push_registrations_devices_fk" FOREIGN KEY (device_id) REFERENCES devices(id)
如果我要拨打user.destroy
,当user
仍然存在user
时,Rails如何尝试销毁PushRegistration
的关联设备参考文献Device
?
混淆Rails如何处理它以及Postgres&#39;观点,谢谢!
编辑以获取更多信息:
class PushRegistration < ActiveRecord::Base
belongs_to :user
belongs_to :device
belongs_to :client
end
class Device < ActiveRecord::Base
has_many :user_tokens
has_many :push_registrations
belongs_to :user
end
答案 0 :(得分:0)
推送注册时外键约束在哪里?
因此,在建立人际关系时,rails会有一些魔力。问题是,您的推送注册是如何设置的?如果push_registrations是属于设备的对象,则它应该仅引用该设备。例如:
class User < ActiveRecord::Base
has_many :devices, dependent: :destroy
has_many :push_registrations, through: :devices
end
class Devices < ActiveRecord::Base
belongs_to :user
has_many :push_registrations, dependent: :destroy
end
class PushRegistration < ActiveRecord::Base
belongs_to :device
has_one :user, through: :device
end
Rails魔术将允许你用这样的东西链接下线
user.devices.each do |device|
device.name
device.push_registration.id
end
您无需在推送注册表上为用户设置外键。有了你的属性,你也可以用另一种方式链接起来然后去
push_registrations.each do |pr|
pr.id
pr.device
end
希望能回答你的问题。如果没有让我知道我可能会失去你!