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是否爲零?