2

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
1
  • Please post your LotReport's source code. Commented Mar 8, 2015 at 0:06

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

I'm going to accept your answer because testing your code out caused my problem to go away. I still have no idea what was causing the issue in the first place. Edit: I did not even modify any of the original code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.