Habtm relation, cannot populate join table

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

On Wed, Apr 21, 2010 at 8:08 AM, Veena J. [email protected]
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/
Github: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

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

On Thu, Apr 29, 2010 at 1:26 AM, Veena J. [email protected]
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/
Github: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale