Robert Sherwood
2006-May-06 03:43 UTC
[Rails] Cant read an Array as an attribute of a record
I am trying to check the size of an array in a newly created object, and
am getting an "undefined method" error. I have written a minimal test
case. Can someone spot the issue here? I''m sure I''m missing
something
simple....
I realize that I''m not saving the object to the database, I''ve
got a
requirement to create and manipulate it in memory before persisting it.
Thanks in advance to anyone who may help!
Test/app/db/migrate/001_create_test_objects.rb
------------------------------------------------------------------
class CreateTestObjects < ActiveRecord::Migration
def self.up
create_table :test_objects do |t|
t.column :name, :string
t.column :test_array, :string
end
end
def self.down
drop_table :test_objects
end
end
Test/app/models/test_object.rb
------------------------------------------------------------------
class TestObject < ActiveRecord::Base
serialize :test_array
def initialize
@test_array = []
puts "Called constructor"
puts @test_array.size
# This Works!!!
end
end
Test/app/controllers/test_object_controller.rb
------------------------------------------------------------------
class TestObjectController < ApplicationController
def test_case
@test_object = TestObject.new
puts @test_object.test_array.size
# This doesn''t work!!!
end
end
http://localhost:3000/test_object/test_case
------------------------------------------------------------------
undefined method `test_array'' for #<TestObject:0x39e4030
@test_array=[]>
RAILS_ROOT: ./script/../config/..
Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/app/controllers/test_object_controller.rb:4:in
`test_case''
-e:3
<...snip...>
Console Log
------------------------------------------------------------------
127.0.0.1 - - [05/May/2006:23:38:33 Eastern Standard Time] "GET
/test_object/test_case HTTP/1.1" 500 7836
- -> /test_object/test_case
Called constructor
0
--
Posted via http://www.ruby-forum.com/.
Apparently Analagous Threads
- Session mgmt. bug - ActiveRecord & MemoryStore session store
- Testing if a variable is specified within a function & adding TRUE/FALSE options to functions
- TukeyHSDs function (pgirmess package)
- 2.3.11.3 on 32bit platforms
- Can not test if model name is Tester in rails 2 (ArgumentError: wrong number of arguments (0 for 1))
