Could someone please explain how to update records that are associated? Here is my problem: Database tables: create table program_specialists ( id int not null auto_increment, first_name varchar(100), last_name varchar(100), primary key (id) ); create table email_addresses ( id int not null auto_increment, username varchar(100), domain varchar(100), primary key (id) ); Model classes: class EmailAddress < ActiveRecord::Base has_one :program_specialist def table_name ''email_addresses'' end def validate errors.add_on_empty %w( username domain ) end end class ProgramSpecialist < ActiveRecord::Base belongs_to :email_address def validate errors.add_on_empty %w( first_name last_name ) end end The view is a very simple page with 4 text fields for entering the data. Creating and viewing ProgramSpecialist records works just fine, but I am unable to properly update. In the controller, when the data comes in on the post and the id field has a value, I do this: @program_specialist = ProgramSpecialist.find(@params[''program_specialist''][''id'']) @program_specialist.email_address.attributes= @params[''email_address''] @program_specialist = ProgramSpecialist.attributes= @params[''program_specialist''] The error is that ProgramSpecialists has no method attributes=. This is somehow because of the belongs_to entry in the ProgramSpecialist class. If I remove all the associations from the models, and do things in a more "brute force" fashion, everything works. I''m sure this is all related to my poor understanding of activerecord associations. Can anyone give me some insight?
There are times when you are a complete fool, and only realize that you are a complete fool after you''ve broadcast your foolishness to everyone. Please ignore my request. I''m not sure what I was doing here, but to me it looked correct (I don''t know why): @program_specialist = ProgramSpecialist.attributes= @params[''program_specialist''] but the correct line is: @program_specialist.attributes= @params[''program_specialist''] Matthew Thill wrote:> Could someone please explain how to update records that are associated? > Here is my problem: > > Database tables: > > create table program_specialists ( > id int not null auto_increment, > first_name varchar(100), > last_name varchar(100), > primary key (id) > ); > > create table email_addresses ( > id int not null auto_increment, > username varchar(100), > domain varchar(100), > primary key (id) > ); > > Model classes: > > class EmailAddress < ActiveRecord::Base > has_one :program_specialist > > def table_name > ''email_addresses'' > end > > def validate > errors.add_on_empty %w( username domain ) > end > end > > class ProgramSpecialist < ActiveRecord::Base > belongs_to :email_address > > def validate > errors.add_on_empty %w( first_name last_name ) > end > end > > The view is a very simple page with 4 text fields for entering the data. > Creating and viewing ProgramSpecialist records works just fine, but I am > unable to properly update. > > In the controller, when the data comes in on the post and the id field > has a value, I do this: > > @program_specialist = > ProgramSpecialist.find(@params[''program_specialist''][''id'']) > @program_specialist.email_address.attributes= > @params[''email_address''] > @program_specialist = ProgramSpecialist.attributes= > @params[''program_specialist''] > > > The error is that ProgramSpecialists has no method attributes=. This is > somehow because of the belongs_to entry in the ProgramSpecialist class. > If I remove all the associations from the models, and do things in a > more "brute force" fashion, everything works. > > I''m sure this is all related to my poor understanding of activerecord > associations. Can anyone give me some insight? > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Matthew, On 18.3.2005, at 20:37, Matthew Thill wrote:> Could someone please explain how to update records that are > associated? Here is my problem: > > Database tables: > > create table program_specialists ( > id int not null auto_increment, > first_name varchar(100), > last_name varchar(100), > primary key (id) > ); > > create table email_addresses ( > id int not null auto_increment, > username varchar(100), > domain varchar(100), > primary key (id) > );You don''t have a foreign key field in there, do you? According to your model, there should be a email_address_id field in your specialist table.> > Model classes: > > class EmailAddress < ActiveRecord::Base > has_one :program_specialist > > def table_name > ''email_addresses'' > endYou shouldn''t need this, as the table name is obeying Rails'' rules. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Matthew Thill wrote:> There are times when you are a complete fool, and only realize that you > are a complete fool after you''ve broadcast your foolishness to everyone. > Please ignore my request.Not to worry. Some time ago I learned an essential bit of Zen wisdom: "The path to true enlightenment begins with clicking ''send''." James Britt