0

Currently I've defined two models, Accounts and Users, within my app using has_and_belongs_to_many associations.

An account within the app refers to an organization such as a company.

I'm able to get the current user easily, but I'm struggling to find a way to build an object to store the current account if the current user has multiple accounts.

Right now I'm defaulting to the first account that belongs to the current user and I'm guessing the solution is going to be along the lines of including the selected account id parameter in my defined routes, but I'm not really sure how to do that since I'm fairly new to RoR and would prefer to set it up behind the scenes if possible without revealing the account id within the URL.

Edit - Adding Code

models/account.rb

class Account < ActiveRecord::Base
    #define relationship to users
    has_and_belongs_to_many :users

    #...
end

models/user.rb

class User < ActiveRecord::Base
    #define relationship to account
    has_and_belongs_to_many :accounts

    #...
end

** Previous 'current_account' implementation before updating accounts and users associations to habtm.

controller/application_controller.rb

class ApplicationController < ActionController::Base
    protect_from_forgery
    helper_method :current_account

    private

    def current_account
        if user_signed_in?
            @current_account ||= Account.find(current_user.account_id)
        end
    end
end

The current_account helper method above no longer works because I've changed my associations from Account has many Users and Users belong to Account TO Accounts has and belongs to many Users and vice versa. For now I've updated the above code to

@current_account ||= current_user.accounts[0]

but obviously that remains as a static variable where I need it to be dynamic depending on which account the user selects.

Also I'm using devise, so I'm able to utilize their current_user helper method to easily get the current user.

Any help would be greatly appreciated!

Mikael Kessler
  • 1,235
  • 1
  • 15
  • 27
  • Could you give some sample code on what you are trying to do? Listing out all the models that are connected to the relation. Habtm relationships seem to be the silver bullet at the beginning but often times turn out to have a simpler answer. – Saifis Jan 20 '14 at 08:15
  • @Saifis - Updated question with what I've implemented so far. – Mikael Kessler Jan 20 '14 at 08:38

1 Answers1

0

Sorry I've been busy.

Going on a limb, from the source you probably do not need a habtm, even if you do you seem to be missing the middle table to hold the associations between the two models. The only way you would need a habtm is when a single account would have several users, which seems kinda odd.

Account

class Account < ActiveRecord::Base
    belongs_to :user
end

User

class User < ActiveRecord::Base
    has_many :users
end

seems better.

However, let me go on and guess on what your trying to get,

probably you want to define an organization, and connect several users to it, in which you would need habtm, the models would be like this,

User

class User < ActiveRecord::Base
  has_many :accounts
  has_many :organizations, through: :accounts
end

Organization

class Organization < ActiveRecord::Base
  has_many :accounts
  has_many :organizations, through: :accounts
end

Account

class Account < ActiveRecord::Base
  has_many :users
  has_many :organizations
end

The Account model would be what you call a middle table(it may be called other things this comes to mind first) and will hold which user is related to an organization and visa versa, it can hold stuff that would not belong in user or organizations, like the account number or user_type specific to the organization.

Theres a Railscast about many to many associations here, if you are not familiar with many to many on rails.

Tips and cavets of many to many relationships are not confined by Rails, so if you are not familiar with them in general, I advise to look for info on them on the internet, with relational databases as the keyword.

Hope this points you in the right direction, please comment if you need more help.

Saifis
  • 2,197
  • 1
  • 22
  • 36