I took over a website that was built by somebody else. I'm now trying to get it up and running on localhost. However, when I migrate it looks like the previous developer put code into the migrations that may rely on a seed already being there. The migrate file looks like this.
def up
add_column :supplies, :color, :string
Supply.where(:title => "Shipped").first.update(:color =>'#e20ce8')
end
def down
remove_column :supplies, :color
end
The error I get on this file when i run rake db:migrate is...
rake aborted!
StandardError: An error has occurred, this and all later migrations
canceled:
undefined method `update' for nil:NilClass
What can I do to fix this?
What about doing rake db:schema:load? I believe that would allow you to get going and then allow you to use rake db:migrate going forward.
What can be happening is that a previous migration which can be seeding supply model is not running or table is truncated. As a good practice we should not seed data with migrations but rather just build the schema with migrations.
You have 2 options:
How about pulling this code and the other seeders in migrations and putting them in seeds.rb and running rake db:seed
#in seeds.rb
Supply.where(:title => "Shipped").first.update(:color =>'#e20ce8')
Or,
Check before updating the migration.
instance = Supply.where(:title => "Shipped").first
instance.update(color: '#e20ce8') if instance.present?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With