# Help with DRY violation

**URL:** <https://www.ruby-forum.com/t/help-with-dry-violation/54938>\
**Category:** Rails\
**Created:** [March 15, 2006, 11:17am UTC](https://www.ruby-forum.com/t/help-with-dry-violation/54938 "2006-03-15T11:17:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mshah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mshah](https://www.ruby-forum.com/u/mshah)\
**Post date:** [March 15, 2006, 11:17am UTC](https://www.ruby-forum.com/t/help-with-dry-violation/54938/1 "2006-03-15T11:17:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mshah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mshah](https://www.ruby-forum.com/u/mshah)\
**Post date:** [March 15, 2006, 2:39pm UTC](https://www.ruby-forum.com/t/help-with-dry-violation/54938/2 "2006-03-15T14:39:58Z")

</div>

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](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![mshah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mshah](https://www.ruby-forum.com/u/mshah)\
**Post date:** [March 15, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/help-with-dry-violation/54938/3 "2006-03-15T18:45:46Z")

</div>

Norman,

That seems to work but, it wont let me set an attribute for when calling  
:create\_table\_b

thanks,  
Manish

---

<div class="post-metadata">

**Author:** ![mshah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mshah](https://www.ruby-forum.com/u/mshah)\
**Post date:** [March 15, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/help-with-dry-violation/54938/4 "2006-03-15T20:07:05Z")

</div>

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](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![mshah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mshah](https://www.ruby-forum.com/u/mshah)\
**Post date:** [March 15, 2006, 9:43pm UTC](https://www.ruby-forum.com/t/help-with-dry-violation/54938/5 "2006-03-15T21:43:11Z")

</div>

that worked great! thanks so much.
