Can't read or write a column in Rails 3.1.0 app

Hi All,

I have a very puzzling problem with a Rails 3.1.0.rc4/.rc5 app and I
could be missing something very obvious.

I have a table (workunits) with a column (worked_seconds) that I can’t
read or write. I’ve run migrations to remove and re-add it and I still
have the issue.

If I manually add a value to the column in the table using Sequel Pro,
AR returns a record that includes the correct value of the column (see
below) but referencing the column via @workunit.worked_seconds returns
nil (see below).

I’d appreciate any thoughts and suggestions.

**Leigh


----[schema.rb]

create_table “workunits”, :force => true do |t|
t.datetime “started_on”
t.datetime “ended_on”
t.integer “user_id”
t.integer “task_id”
t.datetime “created_at”
t.datetime “updated_at”
t.integer “worked_seconds”
end

----[workunit.rb]

class Workunit < ActiveRecord::Base
belongs_to :user
belongs_to :task

attr_accessor :worked_seconds

end

-----[workunits_controller.rb]

def show
@workunit = Workunit.find(params[:id])
session[:current_workunit_id] = @workunit.id
respond_to do |format|
format.html # show.html.erb
format.json { render json: @workunit }
end
end

----[show.html.erb]

<% title “Workunit for: #{@workunit.task.name}” %>
<%= @workunit.inspect %>
======> [produces:
#<Workunit
id: 6
started_on: “2011-07-24 16:00:00”
ended_on: “2011-07-24 18:59:00”
user_id: 1
task_id: 8
created_at: “2011-07-26 01:46:00”
updated_at: “2011-07-26 01:57:44”
worked_seconds: 14400

======> ]


Started on: <%= @workunit.started_on.to_s(:long) %>

Ended on: <%= @workunit.ended_on.to_s(:long) %>

Worked seconds: <%= @workunit.worked_seconds.inspect %> ======> [produces: nil ======> ]

User: <%= @workunit.user.email %>

Task: <%= @workunit.task.name %>

<%= link_to ‘Edit’, edit_workunit_path(@workunit) %> |
<%= link_to ‘Back’, workunits_path %>

----[Gemfile]

source ‘http://rubygems.org

gem ‘rails’, ‘>= 3.1.0.rc4’

gem ‘sqlite3’

gem ‘sass-rails’, “~> 3.1.0.rc”
gem ‘coffee-script’
gem ‘uglifier’
gem ‘rspec-rails’
gem ‘cucumber-rails’
gem ‘mysql2’
gem ‘jquery-rails’
gem ‘webrat’, ‘>= 0.7.2’
gem ‘devise’
gem ‘factory_girl_rails’

group :test do

Pretty printed test output

gem ‘turn’, :require => false
gem ‘database_cleaner’
end

On 26 July 2011 03:59, Leigh D. [email protected] wrote:

class Workunit < ActiveRecord::Base
belongs_to :user
belongs_to :task

attr_accessor :worked_seconds

That is the problem, attr_accessor creates access methods that
override those that access the database columns. Remove this line (or
perhaps you meant attr_accessible?).

Colin

If I manually add a value to the column in the table using Sequel Pro,
attr_accessor :worked_seconds

That is the problem, attr_accessor creates access methods that
override those that access the database columns. Remove this line (or
perhaps you meant attr_accessible?).

Colin

Thank you, Colin!! I was pretty sure it was a case of Rails doing what I
told it to do and not what I meant for it to do.

I did mean “attr_accessible”.

**Leigh