High guys. This isn’t behaving the way I think it should which means I’m doing it wrong;
class Tag < ActiveRecord::Base
has_and_belongs_to_many :properties
end
class Property < ActiveRecord::Base
has_and_belongs_to_many :tags
def amenities
tags.where(:classification => :amenity)
end
end
So I have Properties and Tags. They have a HABTM relationship with a pivot table.
When I do a .tags on a property, I get the full list and if I do a .clear on that full list it correctly removes the associations from the database.
When I do a .amenities I get only those tags that are flagged with the classification of amenity correctly, but if I do a .clear on those results it fails to remove them but rather just does the .amenities query again in the console with an output of [].
So this means it’s just .clear‘ing the result array.. not the association which is what I actually want.
So the question then is; what is the correct way to .clear an association from a HABTM relationship while giving it essentially a where clause to limit which associations are being removed?
Thanks guys. Hope that wasn’t too confusing..
Instead of defining a method querying tags, you could add another tag association with conditions, like:
clear, and any other habtm assocation methods, should work as expected.