Forum: Ruby on Rails help with DRY violation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F10f95926f620318a71c102f207649b7?d=identicon&s=25 Manish Shah (Guest)
on 2006-03-15 11:17
(Received via mailing list)
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.
Bf66e10c8fc4abefebde0425e7f6f15a?d=identicon&s=25 Norman Timmler (Guest)
on 2006-03-15 14:39
(Received via mailing list)
Am Mittwoch, den 15.03.2006, 02:16 -0800 schrieb Manish Shah:
> 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 Timmler

http://blog.inlet-media.de
F10f95926f620318a71c102f207649b7?d=identicon&s=25 Manish Shah (Guest)
on 2006-03-15 18:45
(Received via mailing list)
Norman,

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

thanks,
Manish
Bf66e10c8fc4abefebde0425e7f6f15a?d=identicon&s=25 Norman Timmler (Guest)
on 2006-03-15 20:07
(Received via mailing list)
Am Mittwoch, den 15.03.2006, 09:43 -0800 schrieb Manish Shah:
>         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 Timmler

http://blog.inlet-media.de
F10f95926f620318a71c102f207649b7?d=identicon&s=25 Manish Shah (Guest)
on 2006-03-15 21:43
(Received via mailing list)
that worked great!  thanks so much.
This topic is locked and can not be replied to.