Пользователь может создать организацию, а затем он может сделать других пользователей модераторами в своей организации. Ниже показан способ создания организации.Rails has_many через
def create
@organization = current_user.organizations.build(organization_params)
# Confirm organization is valid and save or return error
if @organization.save!
# New organization is saved
respond_with(@organization) do |format|
format.json { render :json => @organization.as_json }
end
else
render 'new', notice: "Unable to create new organization."
end
end
Как создать модераторы для организации. Я пробовал использовать has_many, но это не удалось. Может кто-нибудь мне помочь?
Update
Организация Модель
class Organization < ActiveRecord::Base
has_many :moderators
has_many :users, :through => :moderators
end
UserModel
class User < ActiveRecord::Base
enum role: [:user, :moderator, :organization, :admin]
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :user
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :moderators
has_many :organizations, :through => :moderators
end
Модератор Модель
class Moderator < ActiveRecord::Base
belongs_to :user
belongs_to :organization
end
Когда я создаю новую организацию, моя организация user_id равна нулю?