Renan Dhein
2014-Mar-10 19:02 UTC
Saving associated objects without whitelisting their _id field
Hello, I've been struggling for a while with a problem. I have the following (slightly simplified) structure: Services # fields: # name has_many :service_city_taxations, :dependent => :destroy has_many :cities, :through => :service_city_taxations accepts_nested_attributes_for :service_city_taxations, :allow_destroy => true ServiceCityTaxations # fields: # service_id # city_id # local_taxation_rate attr_accessible *:city_id*, :city_sales_tax_code, :city_sales_tax_rate, :service_id belongs_to :city belongs_to :service Cities # fields: # name has_many :service_city_taxations, :dependent => :destroy has_many :services, :through => :service_city_taxations Now, I work with services in the services view and I use nested forms to create ServiceCityTaxations directly along with my Services. I create and save them and everything works perfectly well. However, I want to make this work* without having to allow the city_id attribute in my ServiceCityTaxations'* *attr_accessible*, since that's something the user picks himself. If I take city_id out of my attr_accessible parameters, Rails obviously ignores it during my mass assignment (update_attributes or save), since it's an attribute that isn't in my whitelist. I was wondering if there's some way for me to load up my City object in the controller before calling my update_attributes and then save my ServiceCityTaxations with the whole City object instead of only with the id or something like that. That way I could avoid whitelisting city_id at all, but there's also the problem of dealing with nested form results from has_many associations in the controller, since the params hash gets quite messy. I hope I was able to explain my situation. Any help is greatly appreciated. Thanks! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b4491629-263b-4ea7-8b4a-a4414c569374%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.