0
votes

The Signup form in my app allows users to create an account - it also creates their user record and an accounts_users (has_many :through) record. Once they click submit, I am calling the 'setup_account' method to input several default values for the user's new account. With that said, the method is close to working but is inputting incorrect values for the three records where I manually assign values (i.e. :user_id => @user or :account_id => @account). The incorrect value for account_id and user_id always end up being 1. Does anyone have any insight into why this is not working?

Here is my Account model:

  def self.setup_account(p)
    Account.transaction do
      @account = Account.new(p)
      @account.save!
      @user = @account.users.first
      @user.create_profile!
      @group = @account.groups.create!( :user_id => @user.id, :name => 'Default' )
      @group.members.create!( :account_id => @account.id, :user_id => @user.id )
      Role.all.each do |role|
        @account.roles_users.create!( :user_id => @user.id, :role_id => role.id )
      end
    end
    return @account
  end

Here is my AccountsController:

  def new
    @account = Account.new
    @user = @account.users.build()
    @account.accounts_users.build()
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @account }
    end
  end

  def create
    @account = Account.setup_account(params[:account])
    respond_to do |format|
      if [email protected]?
        flash[:domain] = @account.subdomain
        format.html { redirect_to thanks_url }
        format.json { render json: @account, status: :created, location: @account }
      else
        format.html { render action: "new" }
        format.json { render json: @account.errors, status: :unprocessable_entity }
      end
    end
  end

My model associations are:

  • Account (has_many :users; :through account_users; has_many :accounts_users; has_many :groups; has_many :members; has_many :roles_users)
  • User (has_one :profile; has_many :accounts_users; has_many :accounts, :through => :accounts_users; has_many :members; has_many :groups; has_many :roles_users; has_many :roles, :through => :roles_users)
  • Accounts_User (belongs_to :account; belongs_to :user)
  • Group (has_many :members, as: membership; belongs_to :user; belongs_to :account)
  • Member (belongs_to :membership, :polymorphic => true; belongs_to :account; belongs_to :user)
  • Profile (belongs_to :user)
  • Role (has_many :roles_users; has_many :users :through => :roles_users; has_many :accounts, :through => :roles_users)
  • RolesUser (belongs_to :user; belongs_to :account; belongs_to :role)

Edit: Edited the setup_account method and create action.

1

1 Answers

0
votes

Check my comments in your model method,

def self.setup_account(p)
  Account.transaction do
    @account = Account.new(p)
    # Account is not created yet so @account.users won't be retrieved properly, I guess
    # You need to save account first.
    @user = @account.users.first
    @user.build_profile
    @group = @account.groups.build( :user_id => @user, :name => 'Default' ) #Error
    @group.members.build( :account_id => @account, :user_id => @user ) #Error
    Role.all.each do |role| #Error
      @account.roles_users.build( :user_id => @user, :role_id => role.id )
    end
    # Try moving this above my comment.
    @account.save!
  end
end

Since you are using a transaction block, you can start saving things as soon as you build them. In case of any error, it'll be rolled back automatically.