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
精彩评论