Skip to content

Instantly share code, notes, and snippets.

@ruprict
Created November 19, 2010 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruprict/707003 to your computer and use it in GitHub Desktop.
Save ruprict/707003 to your computer and use it in GitHub Desktop.
My first shot at keeping reference associations up to date
class Layer
...blah....
# having to ovewrite update_attributes to make sure the associations get set
def update_attributes(attr)
update_map_config_ids(attr[:map_config_ids]) unless attr[:map_config_ids].nil?
update_layer_type_id(attr[:layer_type_id]) unless attr[:layer_type_id].nil?
super
end
private
def update_map_config_ids(map_config_ids)
map_config_ids.delete("")
self.map_config_ids = map_config_ids.uniq
self.map_config_ids.each do |mc_id|
mc = project.map_configs.find(mc_id)
mc.layer_ids<<self.id unless mc.layer_ids.include?(self.id)
mc.save
end
end
def update_layer_type_id(layer_type_id)
self.layer_type_id=layer_type_id
lt = LayerType.find(layer_type_id)
lt.layer_ids ||= []
lt.layer_ids<<self.id unless lt.layer_ids.include?(self.id)
lt.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment