Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 带Postgres的Rails对值为的列返回nil_Ruby On Rails_Ruby_Postgresql_Activerecord - Fatal编程技术网

Ruby on rails 带Postgres的Rails对值为的列返回nil

Ruby on rails 带Postgres的Rails对值为的列返回nil,ruby-on-rails,ruby,postgresql,activerecord,Ruby On Rails,Ruby,Postgresql,Activerecord,我在schema.rb中声明了此模型: create_table "lot_reports", force: :cascade do |t| t.integer "spaces_occupied" t.integer "spaces_newly_occupied" t.string "name" t.time "time_stamp" t.datetime "created_at", null: false t.datetime "u

我在schema.rb中声明了此模型:

create_table "lot_reports", force: :cascade do |t|
  t.integer  "spaces_occupied"
  t.integer  "spaces_newly_occupied"
  t.string   "name"
  t.time     "time_stamp"
  t.datetime "created_at",            null: false
  t.datetime "updated_at",            null: false
  t.integer  "lot_id"
end
当我尝试访问模型上的spaces_Accounted属性时,即使数据库显示该字段的值,我也会收到nil

LotReport Load (0.5ms)  SELECT  "lot_reports".* FROM "lot_reports"  ORDER BY "lot_reports"."id" ASC LIMIT 1
#=> #<LotReport id: 1, spaces_occupied: 107, spaces_newly_occupied: 153, name: "Cummerata Field", time_stamp: "2000-01-01 23:13:25", created_at: "2015-03-07 23:19:42", updated_at: "2015-03-07 23:19:42", lot_id: 1> 
LotReport.first.spaces_occupied
LotReport Load (0.5ms)  SELECT  "lot_reports".* FROM "lot_reports"  ORDER BY "lot_reports"."id" ASC LIMIT 1
#=> nil
不知道这里发生了什么。这是唯一有这个问题的属性;其他人会回报我所期望的

编辑:这里是LotReport源代码

class LotReport < ActiveRecord::Base
   belongs_to :lot

   def plot_array
    [spaces_occupied, time_stamp]
   end
end

这里有一种诊断方法

请在问题中添加型号代码,例如:

$ cat app/models/lot_report.rb
$ uname -a

$ bin/rails --version

$ bundle exec gem list pg
如果您将使用的版本添加到问题中,可能会对我们有所帮助,例如:

$ cat app/models/lot_report.rb
$ uname -a

$ bin/rails --version

$ bundle exec gem list pg
当您请求按id记录并尝试更改该文件时,您会得到什么确切的输出

$ bin/rails console
> lot = LotReport.find(1)
> p lot.spaces_occupied
> lot.spaces_occupied = 1234
> p lot.spaces_occupied
> lot.save!
> p lot.spaces_occupied
> lot.reload
> p lot.spaces_occupied

谢谢你的编辑!请发布你的LotReport的源代码。我将接受你的回答,因为测试你的代码导致我的问题消失了。我仍然不知道最初是什么导致了这个问题。编辑:我甚至没有修改任何原始代码。