同一源上的 has_many 关系
在此示例中,用户引用了软件模型及其 DAW 范围,两者都与软件模型相关
每当我使用以下代码时 - 每次用户从视图页面更新其数据时,它都会将关联写入软件表而不是用户表。
Software Update (1.5ms) UPDATE "softwares" SET "user_id" = $1, "updated_at" = $2 WHERE "softwares"."id" = $3 [["user_id", 76338], ["updated_at", "2022-03-07 11:54:08.903184"], ["id", 8]]
我要求每个用户存储他们偏好的 daw ID - 但此时它正在将保存的用户的用户 ID 写入软件表中,从而覆盖数据。
软件模型
has_and_belongs_to_many :users
scope :daws, -> {where is_daw:true}
用户模型
has_and_belongs_to_many :softwares
has_many :daws, -> {Software.daws}, :class_name => 'Software'
视图
<%= f.association :daws, label: false, :input_html => { :multiple => false } %>
The User references both the Software Model and it's DAW scope in this example, both relating to the Software model
Whenever i use the below code - every time a user updates their data from the View page it writes the association to the Software table instead of the User table.
Software Update (1.5ms) UPDATE "softwares" SET "user_id" = $1, "updated_at" = $2 WHERE "softwares"."id" = $3 [["user_id", 76338], ["updated_at", "2022-03-07 11:54:08.903184"], ["id", 8]]
I require each user to store the daw ID of their preference - but at the minute it's writing the User ID of the user saving into the Software table which overwrites the data.
Software Model
has_and_belongs_to_many :users
scope :daws, -> {where is_daw:true}
User Model
has_and_belongs_to_many :softwares
has_many :daws, -> {Software.daws}, :class_name => 'Software'
View
<%= f.association :daws, label: false, :input_html => { :multiple => false } %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论