2016-07-07 55 views
0

我创建数据库连接查找和.RB文件,并试图找到记录定义模型,用find查询只工作正常,但查询与findconditions不工作。无法与“ID”(的ActiveRecord :: RecordNotFound)

我的.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应用程序,它是在轨控制台做工精细也是我发现,产生的错误转化ItemSchemaAttributes from ItemSchemaAttribute

+0

为什么不使用'where'而不是'conditions'? – Aleksey

+0

你正在使用哪个版本的Rails? – Pavan

+0

我正在使用Rails 2.3。18 – Amit

回答

0

喔!

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

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

但当我运行ruby程序它指向系统的activerecord这是4.2.1所以find(:first, ...)find(:all, ...)将无法​​正常工作。

+0

这就是为什么您需要使用'bundler' gem来维护所有平台上的** gems和版本**同步 –

1

我认为这是过时的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) 

UPDATE
(其我不推荐)你可以试试activerecord-deprecated_finders宝石。
我不使用它为我自己,但它似乎像这种宝石支持find有条件的哈希

+0

为什么它不能使用'conditions'? – Amit

+1

如果activerecord 4.2.1不支持此语法,它应该如何工作? – Aleksey

2

find(:first, ...)find(:all, ...)一直以来的Rails 3.2.12弃用,并有可能与滑轨取出4 +

你应该要使用正确的成语:

Model.where(conditions)得到一个集合

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

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

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

+0

我在回答执行'find_by_schema_attribute_id'之前尝试,它工作得很好。我得到了activerecord 4.2.6。 – Aleksey

+0

我的不好。我从3.2开始就停止使用动态查找器。但根据http://guides.rubyonrails.org/4_0_release_notes.html#active-record-deprecations:'除find_by _...和find_by _...之外的所有动态方法!我不鼓励使用'Model.find_by()'而不是动态查找器。 – iam3v4n

+0

我同意。我只是把这个代码放在这里以满足每个人的兴趣。这取决于我们使用这种或那种方法) – Aleksey

相关问题