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. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060315/d4ea61d5/attachment-0001.html
Am Mittwoch, den 15.03.2006, 02:16 -0800 schrieb Manish Shah:> 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?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
Norman, That seems to work but, it wont let me set an attribute for when calling :create_table_b thanks, Manish On 3/15/06, Norman Timmler <lists@inlet-media.de> wrote:> > Am Mittwoch, den 15.03.2006, 02:16 -0800 schrieb Manish Shah: > > 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? > > 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060315/1a57db58/attachment.html
Am Mittwoch, den 15.03.2006, 09:43 -0800 schrieb Manish Shah:> Norman, > > That seems to work but, it wont let me set an attribute for when > calling :create_table_b > > thanks, > Manish > > On 3/15/06, Norman Timmler <lists@inlet-media.de> wrote: > > Did you try the following: > > class TableA < ActiveRecord::Base > after_create :create_table_b > endclass TableA < ActiveRecord::Base after_create :initialize_associations def initialize_associations create_table_b(:foo => ''bar'') end end -- Norman Timmler http://blog.inlet-media.de
that worked great! thanks so much. On 3/15/06, Norman Timmler <lists@inlet-media.de> wrote:> > Am Mittwoch, den 15.03.2006, 09:43 -0800 schrieb Manish Shah: > > Norman, > > > > That seems to work but, it wont let me set an attribute for when > > calling :create_table_b > > > > thanks, > > Manish > > > > On 3/15/06, Norman Timmler <lists@inlet-media.de> wrote: > > > > 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060315/940698a3/attachment.html