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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Apr 21, 2010 at 8:08 AM, Veena Jose <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 :userAnd 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: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Apr 29, 2010 at 1:26 AM, Veena Jose <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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_idI 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.> 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 >-- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.