I have created db connection and define models in .rb file and trying to find records, query with find
only is working fine but query with find
, conditions
not working.
my .rb file:
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)
full error:
/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>'
i have created rails app with same structure and it is working fine in rails console also i found that generated error converted ItemSchemaAttributes
from ItemSchemaAttribute
where
instead ofconditions
? – Aleksey