Hi all, I''m working with a legacy sqlserver db and I''m trying to override the insert sql in as defined in the HABTM method below(becasue the join has three primary keys), but I''m getting the error: ''undefined local variable or method `record'' for Project:Class'' has_and_belongs_to_many :people, :class => ''Person'', :join_table => ''ProjectTeamRole'', :foreign_key => ''ProjectID'', :association_foreign_key => ''PersonID'', :insert_sql => "INSERT INTO ProjectTeamRole(ProjectID,PersonID,TeamRoleID) VALUES(#{id}, #{record.PersonID},1)" I can see that #id refers to the current Project object, but how do I refer to the PersonID that will be inserted if not as above?? thanks! rich
Rich Brant wrote:> I''m working with a legacy sqlserver db and I''m trying to override the > insert sql in as defined in the HABTM method below(becasue the join > has three primary keys), but I''m getting the error: ''undefined local > variable or method `record'' for Project:Class'' > > has_and_belongs_to_many :people, > :class => ''Person'', > :join_table => ''ProjectTeamRole'', > :foreign_key => ''ProjectID'', > :association_foreign_key => ''PersonID'', > :insert_sql => "INSERT INTO > ProjectTeamRole(ProjectID,PersonID,TeamRoleID) VALUES(#{id}, > #{record.PersonID},1)" > > > I can see that #id refers to the current Project object, but how do I > refer to the PersonID that will be inserted if not as above??Use single quotes, and refer to the association_foreign_key value as record.id: :insert_sql => ''INSERT INTO ProjectTeamRole(ProjectID,PersonID,TeamRoleID) VALUES(#{id},#{record.id},1)'' -- We develop, watch us RoR, in numbers too big to ignore.
ahhh, cool thanks. that worked. for some reason I was led to believe the association_foreign_key was mapped to something different because of the need to specify the foreign key (as something other than tablename_id). On 2/21/06, Mark Reginald James <mrj@bigpond.net.au> wrote:> Rich Brant wrote: > > > I''m working with a legacy sqlserver db and I''m trying to override the > > insert sql in as defined in the HABTM method below(becasue the join > > has three primary keys), but I''m getting the error: ''undefined local > > variable or method `record'' for Project:Class'' > > > > has_and_belongs_to_many :people, > > :class => ''Person'', > > :join_table => ''ProjectTeamRole'', > > :foreign_key => ''ProjectID'', > > :association_foreign_key => ''PersonID'', > > :insert_sql => "INSERT INTO > > ProjectTeamRole(ProjectID,PersonID,TeamRoleID) VALUES(#{id}, > > #{record.PersonID},1)" > > > > > > I can see that #id refers to the current Project object, but how do I > > refer to the PersonID that will be inserted if not as above?? > > Use single quotes, and refer to the association_foreign_key value as record.id: > > :insert_sql => ''INSERT INTO ProjectTeamRole(ProjectID,PersonID,TeamRoleID) VALUES(#{id},#{record.id},1)'' > > -- > We develop, watch us RoR, in numbers too big to ignore. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- http://brantinteractive.com rbrant@brantinteractive.com 4034 skippack pike v. 267.640.2195 f. 215.689.1454
Rich Brant wrote:> ahhh, cool thanks. that worked. for some reason I was led to believe > the association_foreign_key was mapped to something different because > of the need to specify the foreign key (as something other than > tablename_id).In the context of the join table insert, "record" is the Person record you are adding to the project.people association. The association foreign key value you want in this insert is the primary key of this record. This is always "id" in Rails, even if a different field name and set_primary_key have been used in the Person table and model respectively. -- We develop, watch us RoR, in numbers too big to ignore.