我已经为我的模型添加了一个hstore属性:
class ChangePerDayAverageInterestToHstore < ActiveRecord::Migration
def change
add_column :companies, :per_day_average_interest, :hstore
end
end
现在,当我通过控制台生成项目时,我可以毫无问题地查询它。像这样:
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"
当我保存它,然后查询它,我得到No Method错误:
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>'
我是以错误的方式查询它吗?有什么我想念的吗?
谢谢!
答案 0 :(得分:0)
正如您所看到的,#works, but hangs when using stdout.read(1024)
try:
stdin, stdout, stderr = ssh.exec_command('vi <path\filename.exp>')
to_string = (str(stdout.read(512))).splitlines(True)
for line in to_string:
target.write(line)
stdin, stdout, stderr=ssh.exec_command(':q!')
except:
print("Can't Read File!")
是d
对象,它是使用ActiveRecord::Relation
进行过滤后所有Company
个对象的包装。读者方法where
在per_day_average_interest
模型实例上定义,因此您需要调用,如下所示。
Company
如果您想要所有值d.first.per_day_average_interest
。
答案 1 :(得分:0)
where
语句返回对象集合,并且您的列属于一个对象,因此您需要执行以下操作
d = Company.where(:id => 44).first