activerecord - Rails Migration Prepend Column? -
i'm trying figure out how run migration prepends string beginning of column. in specific case have column called url
stores after domain (e.g. /test.html
). want prepend single string http://google.com
beginning of url. in case of example, resulting string value of url entry http://google.com/test.html
.
how can accomplish migration?
i'm not sure qualifies should put migration; generally, migrations change structure of database, rather change format of data inside of it.
the easiest , quickest way not futz around in database @ all, , instead make url
method of model return "http://google.com#{read_attribute(:url)}"
. if want change data in database, i'd make rake task it, like:
namespace :data task :add_domain model.each |model| model.url = "http://google.com#{model.url}" if model.url !~ /google\.com/ model.save if model.changed? end end end
if must migration you, migration's up
similar internals of rake task. (or call rake task directly.)
Comments
Post a Comment