0
用户可以创建组织,然后他可以让其他用户成为其组织的主持人。下面的方法显示了组织是如何创建的。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,但失败了。有人能帮助我吗?
更新
组织模型
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是否为零?