Search code examples
ruby-on-railspostgresqlhstore

Unable to query hstore attribute in Rails


I've added a hstore attribute to my model:

class ChangePerDayAverageInterestToHstore < ActiveRecord::Migration
  def change
      add_column :companies, :per_day_average_interest, :hstore
  end
end

Now when I generate the item via the console, I am able to query it without issue. Like so:

irb(main):002:0> t = Company.new per_day_average_interest: {1 => 10, 2 => 3}
irb(main):011:0> t.per_day_average_interest 
=> {"1"=>"10", "2"=>"3"}
irb(main):013:0> t.per_day_average_interest["1"]                                                                                  => "10"
irb(main):014:0> t.per_day_average_interest["2"]
=> "3" 

When I save it, then query it, I get a No Method error:

irb(main):028:0> t.save!
(0.2ms)  BEGIN                                                                                                                  SQL (0.3ms)  INSERT INTO "companies" ("created_at", "per_day_average_interest", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["created_at", "2015-06-08 19:24:39.694533"], ["per_day_average_interest", "\"1\"=>\"10\",\"2\"=>\"3\""], ["updated_at", "2015-06-08 19:24:39.694533"]]
(21.4ms)  COMMIT                                                                                                                
=> true 
irb(main):029:0> d = Company.where(:id => 44)
  Company Load (0.5ms)  SELECT "companies".* FROM "companies"  WHERE "companies"."id" = 44
=> #<ActiveRecord::Relation [#<Company id: 44, name: nil, average: nil, created_at: "2015-06-08 19:24:39", updated_at: "2015-06-08 19:24:39", revenue_average: nil, country: nil, job: nil, model_name:
 nil, average_daily_interest: nil, per_day_average_interest: {"1"=>"10", "2"=>"3"}>]>
irb(main):030:0> d.per_day_average_interest
NoMethodError: undefined method `per_day_average_interest' for #<Company::ActiveRecord_Relation:0x007fa16d296dd0>
from /home/action/.gem/ruby/2.1.1/gems/activerecord-4.1.0/lib/active_record/relation/delegation.rb:136:in `method_missing'      
from /home/action/.gem/ruby/2.1.1/gems/activerecord-4.1.0/lib/active_record/relation/delegation.rb:99:in `method_missing'       
from (irb):30
from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/commands/console.rb:90:in `start'                                                                                              
        from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/commands/console.rb:9:in `start'                                                                                               
        from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:69:in `console'                                                                                     
        from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!'                                                                                
        from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>'                                                                                           
        from bin/rails:4:in `require'                                                                                                                                                                  
        from bin/rails:4:in `<main>' 

Am I querying it the wrong way? Is there something I am missing?

Thanks!


Solution

  • The where statement returns the collection of objects and your column belongs to one object so you need to do the following d = Company.where(:id => 44).first