ruby - Writing a rake task to amend current string while preserving the rest of the string Rails 4 -
i trying write rake task remove part of string while preserving rest. tried chomp , slice, unable work. below current rake task , string appear in db. currently, replace entire string whatever listed in pets column. not objective. need remove reference pets while keeping remainder of string in amenities column.
fix_pets.rake
namespace :listings desc 'update old pets in db' task fix_pets: :environment listing.all.each |listing| if listing.amenities == "all pets ok" listing.update(amenities: listing.pets) elsif listing.amenities == "pets upon approval" listing.update(amenities: listing.pets) end end end end
amenities in db:
"central a/c pets ok hardwood floors"
after rake task, amenities field should have:
"central a/c hardwood floors"
probably better use include?
if want conditionally change string.
if listing.amenities.include? 'all pets ok ' listing.update(amenities: listing.amenities.gsub('all pets ok ', '')) end
Comments
Post a Comment