newtonrd
newtonrd

Reputation: 2285

Rails with Postgres returns nil for column with value

I have this model declared in 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

When I try to access the spaces_occupied attribute on the model, I receive nil even when the database shows a value for that field.

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

Not sure what's going on here. This is the only attribute that has this problem; the others return what I expect.

Edit: Here is the LotReport source code

class LotReport < ActiveRecord::Base
   belongs_to :lot

   def plot_array
    [spaces_occupied, time_stamp]
   end
end

Upvotes: 2

Views: 886

Answers (1)

joelparkerhenderson
joelparkerhenderson

Reputation: 35453

Here's a way to diagnose it...

Please add the model code to your question, such as:

$ cat app/models/lot_report.rb

It might help us if you add the versions you're using to your question, such as:

$ uname -a

$ bin/rails --version

$ bundle exec gem list pg

What exact output do you get when you ask for a record by id, and try altering the filed?

$ 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

Upvotes: 1

Related Questions