2016-07-07 53 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

相關問題