user2536938
user2536938

Reputation:

Devise: Unable to solve users scope

I'm creating a simple app, where the user can fill and save some data in a MySQL database.

Everytime I save the data as user1, the user2 can see all the user1 and others users can see each other`s data.

The models are setup ok, with has_many, and belongs_to, and the foreign keys appearing on the tables, but I`m really getting pissed off with this.

Hope someone can help me

Thanks

Upvotes: 0

Views: 69

Answers (1)

MZaragoza
MZaragoza

Reputation: 10111

What I like to do to group segment the data is to set a user to a account.

a account has many users, a user belongs to a account

#model 
class Account < ActiveRecord::Base
  has_many :users
  ...
end

class User < ActiveRecord::Base
  belongs_to :account
  ...
end

now I normal user devise to validate my users

now in your application_controller you can do something like this

#app/controllers/application_controller.rb 
before_filter :current_account
def current_account
  @current_account = current_user.account if current_user
end

now all you have to do is scope the data to the account that it belongs to

def indedx
  @users = @current_account.users # this will only return the users associated with that account
end

I hope that this helps

Upvotes: 1

Related Questions