multi-tenant rails app user-account relationship issue -
multi-tenant rails app user-account relationship issue -
my application main gems: rails 4.1.1, pg, acts_as_tenant, devise , activeadmin.
application description: saas multi-tenant app subdomains (not using postgresql schemas). have 2 models: account , user with:
belongs_to :owner class_name user user belongs_to :account , acts_as_tenant(:account) note: if using acts_as_tenant, not sure need declare belongs_to?
on public domain (www.myapp.dev) user can create new business relationship , take own subdomain. user has status of ‘owner’ particular subdomain/account. after business relationship creation, user redirected custom subdomain sign in. 1 time signed in, owner can create/invite other users bring together account.
on business relationship creation, owner_id correctly saved business relationship record. in activeadmin can filter accounts owners.
the problem account_id not saved in user record. in activeadmin, when seek edit users records add together account, info not saved…
i learning rails please easy :)
my business relationship model :
class business relationship < activerecord::base restricted_subdomains = %w(www) belongs_to :owner, class_name: 'user' validates :owner, presence: true validates :name, presence: true validates :subdomain, presence: true, uniqueness: { case_sensitive: false }, format: { with: /\a[\w\-]+\z/i, message: 'contains invalid characters' }, exclusion: { in: restricted_subdomains, message: 'restricted' } accepts_nested_attributes_for :owner before_validation :downcase_subdomain def self.current_id=(id) thread.current[:account_id] = id end def self.current_id thread.current[:account_id] end private def downcase_subdomain self.subdomain = subdomain.try(:downcase) end end my user model :
class user < activerecord::base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable belongs_to :account acts_as_tenant(:account) validates :lastname, presence: true, allow_nil: false validates :firstname, presence: true, allow_nil: false validates :password, presence: true, allow_nil: false validates :email, presence: true, allow_nil: false #uniqueness: true, def to_s "#{firstname} #{lastname} (#{email})" end end application controller :
class applicationcontroller < actioncontroller::base protect_from_forgery with: :exception before_filter :authenticate_user! #, :set_mailer_host before_filter :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:accept_invitation).concat([:firstname]) end private def current_account @current_account ||= account.find_by(subdomain: request.subdomain) end helper_method :current_account def after_sign_out_path_for(resource_or_scope) new_user_session_path end def after_invite_path_for(resource) users_path end end account controller :
class accountscontroller < applicationcontroller skip_before_filter :authenticate_user!, only: [:new, :create] def new @account = account.new @account.build_owner end def create @account = account.new(account_params) if @account.valid? @account.save redirect_to new_user_session_url(subdomain: @account.subdomain) else render action: 'new' end end private def account_params params.require(:account).permit(:subdomain, :name, owner_attributes: [:lastname, :firstname, :email, :password, :password_confirmation, :account_id]) end end user controller :
class userscontroller < applicationcontroller def index @users = user.all end end and db schema :
activerecord::schema.define(version: 20140619202210) # these extensions must enabled in order back upwards database enable_extension "plpgsql" create_table "accounts", force: true |t| t.string "name" t.string "subdomain" t.datetime "created_at" t.datetime "updated_at" t.integer "owner_id" end create_table "active_admin_comments", force: true |t| t.string "namespace" t.text "body" t.string "resource_id", null: false t.string "resource_type", null: false t.integer "author_id" t.string "author_type" t.datetime "created_at" t.datetime "updated_at" end add_index "active_admin_comments", ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id", using: :btree add_index "active_admin_comments", ["namespace"], name: "index_active_admin_comments_on_namespace", using: :btree add_index "active_admin_comments", ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id", using: :btree create_table "admin_users", force: true |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" end add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true, using: :btree add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true, using: :btree create_table "users", force: true |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" t.integer "account_id" t.string "lastname" t.string "firstname" end add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree end
try:
class business relationship < activerecord::base has_one :account ... class user < activerecord::base acts_as_tenant(:account) ... without belongs_to user model.
ruby-on-rails acts-as-tenant
Comments
Post a Comment