【问题标题】:Couldn't find with 'id' (ActiveRecord::RecordNotFound )找不到“id”(ActiveRecord::RecordNotFound)
【发布时间】:2016-07-12 22:56:49
【问题描述】:

我已经创建了 db 连接并在 .rb 文件中定义模型并尝试查找记录,仅使用 find 查询工作正常,但使用 findconditions 查询不工作。

我的 .rb 文件:

require 'mysql2'
require "active_record"

ActiveRecord::Base.establish_connection(
:adapter=> 'mysql2',
:database=> 'qa_1705',
:username=> 'root',
:password=>''
)

class Company < ActiveRecord::Base
  has_many :item_schema_attributes, :dependent => :destroy
  has_many :schema_attributes, :dependent => :destroy   
end

class SchemaAttribute < ActiveRecord::Base  
  belongs_to :company
  has_many :item_schema_attributes, :dependent => :destroy
end

class ItemSchemaAttribute < ActiveRecord::Base  
  belongs_to :company
  belongs_to :schema_attribute
  belongs_to :line_item  
end

#Works fine :
p Company.find(24)
#<Company id: 24, name: "ABC.pvt.ltd", address: "xyz">

#Not working, throws error:

p ItemSchemaAttribute.find(:first,:conditions => [ "schema_attribute_id = ?", 22])

Couldn't find all ItemSchemaAttributes with 'id': (first, {:conditions=>["schema_attribute_id = ?", 22]}) (found 0 results, but was looking for 2) (ActiveRecord::RecordNotFound)

完全错误:

/home/ac/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-4.2.1/lib/active_record/relation/finder_methods.rb:336:in `raise_record_not_found_exception!': Couldn't find all ItemSchemaAttributes with 'id': (first, {:conditions=>["schema_attribute_id = ?", 22]}) (found 0 results, but was looking for 2) (ActiveRecord::RecordNotFound)
    from /home/ac/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-4.2.1/lib/active_record/relation/finder_methods.rb:479:in `find_some'
    from /home/ac/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-4.2.1/lib/active_record/relation/finder_methods.rb:438:in `find_with_ids'
    from /home/ac/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-4.2.1/lib/active_record/relation/finder_methods.rb:71:in `find'
    from /home/ac/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-4.2.1/lib/active_record/querying.rb:3:in `find'
    from /home/ac/.rvm/gems/ruby-1.9.3-p547/gems/activerecord-4.2.1/lib/active_record/core.rb:128:in `find'
    from connect_db_active_record2.rb:33:in `<main>'

我创建了具有相同结构的 Rails 应用程序,它在 Rails 控制台中运行良好,我还发现生成的错误从 ItemSchemaAttribute 转换为 ItemSchemaAttributes

【问题讨论】:

  • 为什么不用where 而不是conditions
  • 您使用的是哪个版本的 Rails?
  • 我使用的是 Rails 2.3.18
  • 但是你的activerecord是4.2.1!

标签: ruby-on-rails ruby activerecord


【解决方案1】:

find(:first, ...)find(:all, ...) 自 Rails 3.2.12 起已被弃用,并可能随 Rails 4.+ 删除

你应该使用正确的成语:

Model.where(conditions)获取收藏

Model.find_by(conditions)Model.where(conditions).first 获取单个项目。

Model.find() 现在只接受主键值作为参数。

ItemSchemaAttribute.find_by_schema_attribute_id(22) 不再适用于 Rails 4.+

【讨论】:

  • 我在回答之前尝试执行find_by_schema_attribute_id,它工作得很好。我得到了 activerecord 4.2.6。
  • 我的错。自 3.2 起,我才停止使用动态查找器。但根据guides.rubyonrails.org/…All dynamic methods except for find_by_... and find_by_...! are deprecated.,我仍然鼓励使用Model.find_by(),而不是动态查找器。
  • 我同意。我只是把这段代码放在这里,供大家参考。使用一种或另一种方法取决于我们)
【解决方案2】:

我认为它是 activerecord 4.2.1 的过时语法。
在 ActiveRecord 3 中你可以试试

 ItemSchemaAttribute.where(schema_attribute_id: 22).first

在 ActiveRecord 4 中你应该这样做

ItemSchemaAttribute.find_by(schema_attribute_id: 22)

ItemSchemaAttribute.find_by_schema_attribute_id(22)

更新
如果您喜欢使用旧语法(我不推荐),您可以尝试activerecord-deprecated_finders gem。
我自己不使用它,但似乎这个 gem 支持带有条件哈希的 find

【讨论】:

  • 如果 activerecord 4.2.1 不支持此语法,它应该如何工作?
【解决方案3】:

哦!

我得到了答案,问题在于安装在系统中的 activerecord gem 与安装在 rails 应用程序中的 gem。

我在应用中有 activerecord 版本 2.3.18。所以它是应用程序控制台中的工作文件。

但是当我运行 ruby​​ 程序时,它指向系统的 activerecord,即 4.2.1,所以 find(:first, ...)find(:all, ...) 将不起作用。

【讨论】:

  • 这就是为什么您需要使用bundler gem 来维护 gems 以及版本在所有平台之间同步
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 2019-08-01
相关资源
最近更新 更多