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?
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.
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
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
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.