0
votes

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

3
why don't you use where instead of conditions?Aleksey
Which version of the Rails you are using?Pavan
i am using Rails 2.3.18dr. strange
But your activerecord is 4.2.1!Aleksey

3 Answers

2
votes

find(:first, ...) and find(:all, ...) has been deprecated since Rails 3.2.12 and likely removed with Rails 4.+

You should be using the right idioms:

Model.where(conditions) to get a collection

and

Model.find_by(conditions) or Model.where(conditions).first to get a single item.

Model.find() now only accepts a primary key value as a parameter.

And ItemSchemaAttribute.find_by_schema_attribute_id(22) no longer works for Rails 4.+

2
votes

I think it is obsolete syntax for activerecord 4.2.1.
In ActiveRecord 3 you could try

 ItemSchemaAttribute.where(schema_attribute_id: 22).first

In ActiveRecord 4 you should do

ItemSchemaAttribute.find_by(schema_attribute_id: 22)

or

ItemSchemaAttribute.find_by_schema_attribute_id(22)

UPDATE
If you like to use old syntax (which I do not recommend) you can try activerecord-deprecated_finders gem.
I don't use it for myself but it seems like this gem supports find with conditions hash

0
votes

Ohh!

i got the answer, the problem is with activerecord gem installed in system vs gem installed in rails application.

i have activerecord version 2.3.18 in app. so it is working file in app console.

but when i run ruby program it points to system's activerecord which is 4.2.1 so find(:first, ...) and find(:all, ...) will not work.