I'm trying to update a field in using update_all. However I need the value to be taken from another field which is re-written to my specific format.
If I have something like this in my model:
def self.clean_mac_address()
clean_mac_address = :macaddress.gsub(/[^0-9a-z]/i, '')
end
When I run this:
Radacct.update_all("mac_clean = #{clean_mac_address}")
I get an error:
NoMethodError: undefined method `gsub' for :macaddress:Symbol
Any thoughts how I can do this? Or is there a simpler way to update the field?
gsubmethod on asymbol. May be thats the reason you are getting error.gsubon a symbol? Second, if I understand correctly you want to calculate and fillmac_cleancolumn based on other column for every record. In that case you can useupdate_allonly if you perform all manipulation in SQL asupdate_allwill run the update you're passing to it as a string on all records, but it won't load them from DB and won't initialize model instances. That means you're dealing with SQL queries and do not have direct access toActiveRecordattributes.params[:id]. What is :macaddress in above code?