Help with DRY violation

I’m trying to be a good rails developer and fix DRY violations as i find
them. However, i’m trying to fix this one, i cant seem to get it to
work.

I have two tables A and B that have a one-to-one relationship. Table B
belongs_to Table A. Table A has_one Table B. I’m creating instances of
Table A in different places, depending on the controller. However, for
each
time i create a Table A instance, i also need to create an empty Table B
instance.

I tried to incorporate this into the model for Table A using various
callbacks, after_create, after_save, before_create. Trying to set the
table_b attribute to a new Table B instance and have table_b’s foreign
key
be the id the table_a object being created.

However, i kept running into exceptions "undefined method
‘construct_sql’.
I checked my dev log and saw there was an INSERT query for table_b just
after table_a’s INSERT, which is what i wanted. But it dies right
after.
Why is that?

So do i need to just call

table_a_inst.table_b = Table_B.new()
table_a_inst.save

everytime i want to create a Table_A instance?

thanks in advance.

Am Mittwoch, den 15.03.2006, 02:16 -0800 schrieb Manish S.:

I tried to incorporate this into the model for Table A using various

table_a_inst.table_b = Table_B.new()
table_a_inst.save

everytime i want to create a Table_A instance?

Did you try the following:

class TableA < ActiveRecord::Base
after_create :create_table_b
end

If this fails, as you describe above, can you please show us the
callback.


Norman T.

http://blog.inlet-media.de

Norman,

That seems to work but, it wont let me set an attribute for when calling
:create_table_b

thanks,
Manish

Am Mittwoch, den 15.03.2006, 09:43 -0800 schrieb Manish S.:

    Did you try the following:
    
    class TableA < ActiveRecord::Base
      after_create :create_table_b
    end

class TableA < ActiveRecord::Base
after_create :initialize_associations

def initialize_associations
create_table_b(:foo => ‘bar’)
end
end


Norman T.

http://blog.inlet-media.de

that worked great! thanks so much.