# Habtm relation, cannot populate join table

**URL:** <https://www.ruby-forum.com/t/habtm-relation-cannot-populate-join-table/187304>\
**Category:** Rails\
**Created:** [April 21, 2010, 2:08pm UTC](https://www.ruby-forum.com/t/habtm-relation-cannot-populate-join-table/187304 "2010-04-21T14:08:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![veena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/51bf81/32.png) [@veena](https://www.ruby-forum.com/u/veena)\
**Post date:** [April 21, 2010, 2:08pm UTC](https://www.ruby-forum.com/t/habtm-relation-cannot-populate-join-table/187304/1 "2010-04-21T14:08:32Z")

</div>

Hai friends,  
I hav two models related with habtm

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :station,  
:join\_table =\> “stations\_users”,  
:foreign\_key =\> “user\_id”,  
:association\_foreign\_key =\> “station\_id”

class Station \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :user  
end

When i create a new user its user\_id and the station \_id is not inserted  
into the “stations\_users” table.I hav used scaffolding 4 creating these  
two models.How can i insert the fields for the join table also when a  
user is created?  
Can any one help me?

Thanks and regards,  
Veena

---

<div class="post-metadata">

**Author:** ![veena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/51bf81/32.png) [@veena](https://www.ruby-forum.com/u/veena)\
**Post date:** [April 22, 2010, 3:39pm UTC](https://www.ruby-forum.com/t/habtm-relation-cannot-populate-join-table/187304/2 "2010-04-22T15:39:17Z")

</div>

On Wed, Apr 21, 2010 at 8:08 AM, Veena J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hai friends,  
> I hav two models related with habtm
> 
> class User \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :station,  
> :join\_table =\> “stations\_users”,  
> :foreign\_key =\> “user\_id”,  
> :association\_foreign\_key =\> “station\_id”

Shouldn’t this be  
has\_and\_belongs\_to\_many :stations

The plurality is important. Also with that change the join\_table,  
foreign\_key and association\_foreign key options are unnecessary since  
those are the defaults.

> class Station \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :user

And this should be  
has\_and\_belongs\_to\_many :users

> end
> 
> When i create a new user its user\_id and the station \_id is not inserted  
> into the “stations\_users” table.I hav used scaffolding 4 creating these  
> two models.How can i insert the fields for the join table also when a  
> user is created?  
> Can any one help me?

Not sure that this is your problem, but I wouldn’t be surprised.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![veena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/51bf81/32.png) [@veena](https://www.ruby-forum.com/u/veena)\
**Post date:** [April 29, 2010, 7:26am UTC](https://www.ruby-forum.com/t/habtm-relation-cannot-populate-join-table/187304/3 "2010-04-29T07:26:28Z")

</div>

Hai Rick,

I have made the change you hav told. Still the join table is not been  
populated.  
These are my migration data.Is there any problem with this?  
…  
class CreateNewStations \< ActiveRecord::Migration  
def self.up  
create\_table :stations do |t|  
t.string :name  
t.integer :district\_id  
t.string :MDT\_id  
t.string :sim\_no  
t.decimal :longitude, :precision=\>10, :scale=\>6  
t.decimal :latitude, :precision=\>10, :scale=\>6  
t.integer :user\_id  
t.timestamps

```
end
create_table :stations_users, :id => false do |t|
  t.integer :user_id
  t.integer :station_id
end

```

end

def self.down  
drop\_table :stations\_users rescue nil  
drop\_table :stations  
end  
end  
…

class CreateUsers \< ActiveRecord::Migration  
def self.up  
create\_table :users do |t|  
t.string :name  
t.string :username  
t.string :hashed\_password  
t.string :salt  
t.string :status  
t.string :phone  
t.string :email  
t.integer :station\_id  
t.timestamps  
end  
end

def self.down  
drop\_table :users  
end  
end

Thanks and Regards,  
Veena

---

<div class="post-metadata">

**Author:** ![veena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/51bf81/32.png) [@veena](https://www.ruby-forum.com/u/veena)\
**Post date:** [April 29, 2010, 7:55pm UTC](https://www.ruby-forum.com/t/habtm-relation-cannot-populate-join-table/187304/4 "2010-04-29T19:55:58Z")

</div>

On Thu, Apr 29, 2010 at 1:26 AM, Veena J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> t.integer :district_id
> t.string :MDT_id
> t.string :sim_no
> t.decimal :longitude, :precision=>10, :scale=>6
> t.decimal :latitude, :precision=>10, :scale=>6
> t.integer :user_id
> 
> ```

I don’t think you want this field. This would be there if you had

```
class Stations < ActiverRecord::Base
    belongs_to :user
end

```

but you indicated that each station could have many users, and each  
user could have many stations, thus the habtm relations.

> drop\_table :stations\_users rescue nil  
> t.string :hashed\_password  
> drop\_table :users  
> end  
> end

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
