Last active
December 28, 2015 20:49
-
-
Save tanraya/7560409 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Организация | |
class Organization < ActiveRecord::Base | |
has_many :organization_members, class_name: 'Organization::Member', dependent: :destroy | |
has_many :members, through: :organization_members | |
attr_accessible :name | |
end | |
# Член организации | |
class Organization::Member < ActiveRecord::Base | |
belongs_to :organization | |
belongs_to :member, class_name: 'User', foreign_key: :member_id | |
attr_accessible :organization, :organization_id, :member_id | |
end | |
# Пользователь | |
class User < ActiveRecord::Base | |
has_one :organization_member, class_name: 'Organization::Member', foreign_key: :member_id | |
has_one :organization, through: :organization_member | |
end | |
# Контроллер | |
class OrganizationsController < ApplicationController | |
def create | |
@organization = Organization.new(params[:organization]) | |
# Делаем текущего пользователя членом организации | |
# !!! Вот это место мне не нравится. Хотел сделать так: | |
# current_user.create_organization(params[:organization]) - но так не работает. | |
if @organization.save | |
current_user.create_organization_member(organization: @organization) | |
end | |
respond_with @organization, location: organization_path | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment