레일 5와 포스트 그리드 9.5를 사용하고 있습니다. 내가 만들거나 고유 제한 조건입니다 제목을 기반으로 내 dB/씨앗 파일에 모델을 업데이트하거나 싶은 다음 필드와 모델 ...모델의 속성 중 하나를 기반으로 모델을 만들거나 보급하려면 어떻게해야합니까? (Rails 5)
title | character varying |
있습니다. 그래서 나는 시도했다
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])
그러나 나는 아래의 오류가났다. 모델을 만들거나 업데이트하고 나열된 특성을 제외시키는 올바른 방법은 무엇입니까?
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)
시도 :'Scenario.find_or_initialize_by 여기
이 방법에 대한 API를 문서에 대한 링크입니다 (제목 : s.title)' –