开发者

How can I unjoin a join table relationship in rails/console?

开发者 https://www.devze.com 2023-02-25 17:32 出处:网络
I have Users开发者_高级运维 who have and belong to many Objects. So if I type : User.find(2).objects

I have Users开发者_高级运维 who have and belong to many Objects.

So if I type :

User.find(2).objects

A series of objects will return.

How can I clear the relationship this User has with those objects, but not delete the objects.


All I know is to hit this from both sides like so :

First this :

@a = User.find(2)
@a.clear
@a.save

Then this :

Object.all.each{|a| a.users.delete(User.find(2)) if a.users.include?(User.find(2))}


Set the relationship to an empty array.

User.find(2).tap do |u|
  u.objects = []
  u.save!
end
0

精彩评论

暂无评论...
验证码 换一张
取 消