has many through - Rails User Groups - Set Group Owner In Another Model -


i have user created groups in application. i'm confused how set user creates group owner. want there able multiple owners it's 'has-many-through' relationship. can create/edit/delete group.

so question how insert current user_id , group_id group_owners table @ time group created?

here have works far:

user model

class user < activerecord::base    devise :database_authenticatable, :registerable,      :recoverable, :rememberable, :trackable, :validatable, :confirmable    has_many :group_owners   has_many :user_groups, through: :group_owners  end 

group model

class usergroup < activerecord::base     has_many :goup_owners    has_many :users, through: :groups_owners     validates :name, presence: true, length: {minimum: 5}    validates :visibility, presence: true, length: {minimum: 5}     visibility_types = ["public", "private"]  end 

group owner model

class groupowner < activerecord::base     belongs_to :user    belongs_to :user_group  end 

user groups controller - create action

def create    @usergroup = usergroup.new(usergroup_params)    if @usergroup.save      redirect_to user_groups_path    else      render 'new'    end end 

i assume needs go in user group create method i'm not sure what.

thanks can offer.

you should create usergroup like

def create  @usergroup = current_user.user_groups.build(usergroup_params)  if @usergroup.save    redirect_to user_groups_path  else   render 'new'  end end 

this way user group created current users id , group id group owners table.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -