Weblog - 1 items for ActiveRecord
ActiveRecord Has many through and assignment
With Has many through associaitions if you do a simple assignment of an entire collection it does not work.
eg.
2 models Asset and Client have a has_many through relation ship with each other through AssetClient.
The following does not work
@asset.clients = @clients
So a method is needed in Asset like so to make it work.
def clients=(new_clients)
new_clients.each do |item|
clients << item unless clients.include?(item)
end
clients_to_remove = clients - new_clients
clients_to_remove.each do |item|
clients.delete(item)
end
end
