rails-multitenant
rails-multitenant copied to clipboard
Errors when using accepts_nested_attributes_for
I am trying to use Rails-multitenant, but I am running into an issue when creating accounts. I have accounts which can have users and websites underneath. This means that for the website and user I have set:
class Website < ApplicationRecord
include RailsMultitenant::MultitenantModel
belongs_to :account
accepts_nested_attributes_for :account
multitenant_on :account_id
end
class User < ApplicationRecord
include RailsMultitenant::MultitenantModel
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:confirmable, :lockable, :trackable
belongs_to :account
accepts_nested_attributes_for :account
multitenant_on :account_id
end
class Account < ApplicationRecord
has_many :users, dependent: :destroy, inverse_of: :account
has_many :websites, dependent: :destroy, inverse_of: :account
accepts_nested_attributes_for :users, allow_destroy: true
accepts_nested_attributes_for :websites, allow_destroy: true
end
When creating the account I set the user and website and saves it:
class AccountsController < ApplicationController
#layout 'session'
def new
@account = Account.new
@account.users.build
@account.websites.build
end
def create
@account = Account.new(register_account_params)
if @account.save
user = @account.users.last
sign_in(user)
redirect_to root_path, notice: 'Account was successfully created.'
else
render :new
end
end
private
def register_account_params
params.require(:account).permit(:name, users_attributes: [:name, :email, :password, :password_confirmation], websites_attributes: [:url])
end
end
When signing up I get the errors:
- Users account can't be blank
- Websites account users account can't be blank
- Websites account can't be blank
The errors don't appear when disabling the multitenant functionality. My assumption is that this due to me trying to save the user and website through the account. Is there any way to get around this limitation?