3

I am using activeadmin and need to figure out how to require a scope to only show records pertaining to the current user.

I also have other scopes that users can select, but those scopes need to be "pre-scoped" so to speak so that only records belonging to that user are available at any given time.

I hope this makes sense. I'm fairly new to all of this, so i'm not real sure where to start. Thanks in advance for any help.

Sean
  • 1,078
  • 14
  • 25

1 Answers1

1

Did you try scoping with scope_to :current_user ?

AA has some examples with docs . Here they are http://activeadmin.info/docs/2-resource-customization.html#scoping_the_queries

current_user is helper method to get currently logged in user (currend_admin_user is default I think) code from AA initializer

# This setting changes the method which Active Admin calls
# to return the currently logged in user.
config.current_user_method = :current_user

If you had some kind of metod in your model that use your logged in user you can do something like that

controller do
      def scoped_collection
        Post.some_method(current_user)
        #or for example Post.select(current_user.visible_posts_columns) ... etc
      end
    end
Fivell
  • 11,829
  • 3
  • 61
  • 99
  • Ok, i didn't know about the scope_to part, i'll check it out tonight. I do have custom authentication as well as cancan that I am using. I'm not sure if that affects things or not, but I thought that the new version of AA 0.5.0 was supposed to somehow use cancan to only show records that apply. Is there a config in initializers that I need to set for that? Have you used 0.5.0 for that at all? Thanks again! – Sean Oct 24 '12 at 22:48
  • If I understand you can use scoped_collection method to "pre-scope" your data – Fivell Oct 25 '12 at 06:00