开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜