Par*_*oid 5 activerecord ruby-on-rails associations
我有3个型号:
时间表
belongs_to :service
Run Code Online (Sandbox Code Playgroud)
服务
belongs_to :user, :foreign_key => "owner"
has_many :shedules, :dependent => :destroy
Run Code Online (Sandbox Code Playgroud)
用户
has_many :services, :foreign_key => "owner", :dependent => :destroy
has_many :shedules, :through => :services, :foreign_key => "owner", :dependent => :destroy
Run Code Online (Sandbox Code Playgroud)
然后我可以通过以下方式查看用户的所有时间表my_user.shedules
我的目标是删除用户的所有时间表,然后我尝试my_user.shedules.delete_all
但收到此错误:
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection:无法修改关联“User#shedules”,因为源反射类“Shedule”通过 :has_many 与“Service”关联。
经过一番谷歌搜索后,我发现了这个和其他一些类似的帖子,但它们都不适合我的问题。
当然,我可以通过迭代用户服务来删除它,例如
my_user.services.each do |s|
s.shedules.delete_all
end
Run Code Online (Sandbox Code Playgroud)
但我很有趣为什么my_user.shedules.delete_all
不工作。
谢谢!
尝试:-
my_user.shedules.destroy_all
Run Code Online (Sandbox Code Playgroud)
或者
Shedule.delete_all(:owner => my_user.id)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1700 次 |
最近记录: |