activerecord - Rails many_to_many table not updating -


i'm not able many_to_many table update when create new site record. sites table updated, not sites_users. i'm expecting user_id , site_id added sites_users table when site created.

my migration:

class createtablesiteuser < activerecord::migration def   create_table :sites_users, :id => false |t|       t.integer :site_id       t.integer :user_id   end 

models:

user.rb

class user < activerecord::base attr_accessible  :name, :email, :password, :password_confirmation, :user_id has_secure_password has_and_belongs_to_many :sites 

site.rb

class site < activerecord::base attr_accessible :name, :site_key, :organization_id, :user_id, :site_id belongs_to :organization has_and_belongs_to_many :users 

sites_controller.rb

def create   @site = site.new(params[:site])     @site.organization_id = organization.where(:user_id => current_user.id)     @user_id = current_user.id     if @site.save         flash[:success] = "new site created!"         redirect_to root_path     else 

what else need site_id , user_id save sites_users join table?

i have read as possible on many many , has many through topics posted, watched railscast video, , searched as can. appreciated.

thanks!

if creating new site on create action, , want assign site current_user, try following

if @site.save   @site.users << current_user   flash[:success] = "new site created!"   redirect_to root_path else   ... 

this adds current_user list of users of @site after @site passes validations , gets saved.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -