我創建數據庫連接查找和.RB文件,並試圖找到記錄定義模型,用find
查詢只工作正常,但查詢與find
,conditions
不工作。無法與「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
爲什麼不使用'where'而不是'conditions'? – Aleksey
你正在使用哪個版本的Rails? – Pavan
我正在使用Rails 2.3。18 – Amit