doug livesey
2005-Oct-25 09:58 UTC
initialising an instance variable on ActiveRecord creation
Hallo, I have a table like this: create table properties( id bigint unsigned not null auto_increment, property_variable_stack_id bigint unsigned not null, primary key(id), foreign key (property_variable_stack_id) references property_variable_stacks (id)) type=InnoDB; Here you can see a reference to a table property_variable_stacks.>From outside the Property class I can set up a Property with aPropertyVariableStack like this: p=Property.new p.property_variable_stack=PropertyVariableStack.new p.save! This works just fine. However, I have not been able to create a new PropertyVariableStack automatically for each new Property -- I have tried before_create, after_create, initialize... All to no avail. For example, the code I tried for initialize in Property was: def initialize @property_variable_stack=PropertyVariableStack.new end When this ran, I could trace all the correct SQL being generated in the logs, but it raised an error: test_sample(TestPropertyLoader) [Z:/sics/test/tc_property_loader.rb:66]: Exception raised: Class: <NoMethodError> Message: <"undefined method `construct_sql'' for #<PropertyVariableStack:0x2c77d0 8>"> Can anyone tell me why this is? & maybe advise on how to achieve what I''m after? Thanks in advance, doug.
Mark Reginald James
2005-Oct-26 15:15 UTC
Re: initialising an instance variable on ActiveRecord creation
doug livesey wrote:> However, I have not been able to create a new PropertyVariableStack > automatically for each new Property -- I have tried before_create, > after_create, initialize... All to no avail. > For example, the code I tried for initialize in Property was: > > def initialize > @property_variable_stack=PropertyVariableStack.new > enddef initialize super self.property_variable_stack = PropertyVariableStack.new end -- We develop, watch us RoR, in numbers too big to ignore.