I beat my head against a wall for almost 2 hours with this issue.
My first problem was easily solved. I wanted a class that could use
ActiveRecord’s validation capabilities but without using a table behind
it (I would be persisting setup information in the session, but the
initial setup parameters come from a user input form)
No problem…found this link which got me up and running
http://rails.techno-weenie.net/tip/2005/11/19/validate_your_forms_with_a_table_less_model
This was the class definition I ended up with:
class Setup < ActiveRecord::Base
def self.columns() @columns ||= []; end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s,
default, sql_type.to_s, null)
end
…
column :turn, :integer
…
def change_turn
if turn==0
turn = 1
else
turn = 0
end
end
end
ok…
now the head-banger. Any time I called the ‘change_turn’ method inside
my new class, the turn wouldn’t actually be changed. Oh, I would put
logger_debug statements inside the change_turn method itself and the
value WAS being changed, but the changes weren’t visible OUTSIDE the
class. Er?
It wasn’t until I changed the ‘change_turn’ method to look like this
that it started working properly:
def change_turn
if turn==0
self[:turn] = 1
else
self[:turn] =0
end
end
why!!!??? If I can reference the ‘turn’ attribute simply by calling it
'turn (as in ‘if turn==0’) then why cant I set the value by referencing
it the same way (by name)???
Is this in some way related to my abuse of ActiveRecord? Or is this
true of any Ruby class?
My head hurts and I am tired. I assume that I am missing something
basic here…
Any insight would be appreciated.