我想开始使用函数first_or_create,我在ActiveRecord API和http://guides.rubyonrails.org/active_record_querying.html#find-or-build-a-new-object指南中找到了它。但是,Rails假装不承认这个功能:
irb(main):017:0> EoWord.where("id = 110")
=> [#<EoWord id: 110, word: "abatino", wordtype: "substantivo", topic: "pregado, homo">]
irb(main):018:0> EoWord.where("word = 'abatino'").first_or_create
NoMethodError: undefined method `first_or_create' for #<ActiveRecord::Relation:0x1ccf578>
from E:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/relation.rb:374:in `method_missing'
from (irb):18
from E:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from E:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from E:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
我在Windows下运行Rails 3.0.7。非常感谢您的帮助!
答案 0 :(得分:4)
回答你的问题:是的,这个方法在Rails 3.0.7中是未定义的。
根据APIDock's documentation,此方法仅存在于Rails 3.2.1中。您需要升级您的Rails版本才能获得它。