rails: convert a string with regex and write to database
I have an application that accepts an address and writes it to the db. I then want to take that address and convert it to something I can send through Google Maps, so I need to replace all the spaces with "+" symbols. I understand how to do that with a regex:
address.gsub(/\s/, "+")
And can create a variable that doe开发者_运维问答s it, voila. But I want the converted address to live in the DB as well, so it doesn't have to be processed every time. I'm not sure how I process that when I'm creating the entry to begin with and save it to the db as a separate entity ("gmapaddress" or something).
Thanks!
given a table name of rails_db_table and columns userid and gmapaddress and instance vars @gmapaddress and @userid it's as simple as
UPDATE rails_db_table
SET gmapaddress=@gmapaddress
WHERE userid=@userid
of course a more rails way of doing this is with active_record that allows a construct such as:
@user.update_attribute :gmapaddress, @gmapaddress
@user.save
精彩评论