我创建数据库连接查找和.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