# HABTM :insert\_sql help

**URL:** <https://www.ruby-forum.com/t/habtm-insert-sql-help/54064>\
**Category:** Rails\
**Created:** [February 21, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/habtm-insert-sql-help/54064 "2006-02-21T22:29:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![richblunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5e925/32.png) [@richblunt](https://www.ruby-forum.com/u/richblunt)\
**Post date:** [February 21, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/habtm-insert-sql-help/54064/1 "2006-02-21T22:29:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![richblunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5e925/32.png) [@richblunt](https://www.ruby-forum.com/u/richblunt)\
**Post date:** [February 22, 2006, 12:26am UTC](https://www.ruby-forum.com/t/habtm-insert-sql-help/54064/2 "2006-02-22T00:26:40Z")

</div>

Rich B. wrote:

> ```
> :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.

---

<div class="post-metadata">

**Author:** ![richblunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5e925/32.png) [@richblunt](https://www.ruby-forum.com/u/richblunt)\
**Post date:** [February 22, 2006, 3:45am UTC](https://www.ruby-forum.com/t/habtm-insert-sql-help/54064/3 "2006-02-22T03:45:57Z")

</div>

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 J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > :foreign_key => 'ProjectID',
> > 
> > ```
> 
> :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  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
[http://brantinteractive.com](http://brantinteractive.com)  
removed\_email\_address@domain.invalid  
4034 skippack pike  
v. 267.640.2195  
f. 215.689.1454

---

<div class="post-metadata">

**Author:** ![richblunt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5e925/32.png) [@richblunt](https://www.ruby-forum.com/u/richblunt)\
**Post date:** [February 22, 2006, 8:15am UTC](https://www.ruby-forum.com/t/habtm-insert-sql-help/54064/4 "2006-02-22T08:15:49Z")

</div>

Rich B. 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.
