How do I create or update a model based on one of its attributes? (Rails 5)

286 views Asked by At

I’m using Rails 5 and PostGres 9.5 . I have a model with the following field …

 title             | character varying           | 

I would like to either create or update a model in my db/seeds file based on its title, which is a unique constraint. So I tried

scenario = Scenario.find_or_initialize_by_title(s.title)
scenario.update_attributes(Hash[*@record.attributes.except('created_at','updated_at','id').map { |a| [a.first, nil] }.flatten])

but I got the below error. What’s the correct way to create or update my model and also exclude the listed attributes?

localhost:sims nataliab$ rake db:seed
rake aborted!
NoMethodError: undefined method `find_or_initialize_by_title' for #<Class:0x007fb22d28f820>
Did you mean?  find_or_initialize_by
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0.1/lib/active_record/dynamic_matchers.rb:21:in `method_missing'
/Users/nataliab/Documents/workspace/sims/db/seed/scenarios.rb:165:in `block in <top (required)>'
/Users/nataliab/Documents/workspace/sims/db/seed/scenarios.rb:164:in `each'
/Users/nataliab/Documents/workspace/sims/db/seed/scenarios.rb:164:in `<top (required)>'
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
/Users/nataliab/Documents/workspace/sims/db/seeds.rb:10:in `<top (required)>'
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `block in load'
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0.1/lib/rails/engine.rb:549:in `load_seed'
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0.1/lib/active_record/tasks/database_tasks.rb:268:in `load_seed'
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0.1/lib/active_record/railties/databases.rake:196:in `block (2 levels) in <top (required)>'
/Users/nataliab/.rvm/gems/ruby-2.3.0/gems/rake-11.3.0/exe/rake:27:in `<top (required)>'
/Users/nataliab/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/Users/nataliab/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
2

There are 2 answers

1
Paweł Dąbrowski On

Try Scenario.find_or_initialize_by(title: s.title)

7
Taryn East On

The error message gives you the clue:

NoMethodError: undefined method `find_or_initialize_by_title' for #<Class:0x007fb22d28f820>
Did you mean?  find_or_initialize_by

The dynamic method names are now not really "Rails-approved" you should instead use find_or_initialize_by. In this case you'd use:

Scenario.find_or_initialize_by(title: s.title)

Here's a link to the api doc on this method: http://apidock.com/rails/v4.0.2/ActiveRecord/Relation/find_or_initialize_by