Rails 3, CanCan to limit all query results throughout the app based on User's InstanceID?

Up vote 0 down vote favorite share g+ share fb share tw.

I have two tables Users (name, email, password, instance_id, etc...) example: james bond, james@abc.com" rel="nofollow">abc.com, 1 Instance (id, domain) example: 1, abc.com" rel="nofollow">abc.com Through out the application I want to make sure James Bond only sees data that is assigned to his instance = 1 So if I have a books table with (name, desc, instance_id), he only sees his instance's books. I'm told CanCan is the way to go for this but I don't see how to make something set globally like the above, it seems more role based like admin, moderator, etc... Which I'll need but at a level lower than the data rules mentioned above. Thoughts?

Ruby-on-rails ruby-on-rails-3 devise cancan link|improve this question asked Sep 7 '10 at 17:55AnApprentice5,856446165 70% accept rate.

You could try using scopes like: class Books user. Instance_id) } # .... code ... end Now you can do: @personal_books = Books. Personal(@user) you can read more about scopes here: railsdispatch.com/posts/rails-3-makes-li... (scroll down a little bit) Another way is to make an association like User has_many Books through Instance Then you'd have an additional legacy table mapping user to books over their instance.

It's an n:m relation. So a book could belong to multiple instances. It would look like this: User Table: Users (name, email, password, instance_id, etc...) Book Table: Books (name, desc) # no more instance_id!

Mapping Table: Assignments (instance_id, book_id) And your Models would look like: class Assignment :assignments end class Books :assignments end class User Find(:all).

Thanks, but I curious why use an assignments table, that would end up being a big database if every table needs an assignments table to map to the instance table. Why not just add instance_id to all records in the DB? – AnApprentice Sep 7 '10 at 20:03 Because you would end up having lots of duplicates in your database.

A book is a "unique" object and can belong to many instances, right? So you should have only one entry for the book. But many instances can have access to this book right?

So you should have an extra table telling which instances have access to certain books. This is called a many-to-many relationship or n:m relationship. – sled Sep 7 '10 at 22:41 Sled, a book only belongs to 1 instance which is why I don't know if the assignment table is the way to go.

– AnApprentice Sep 8 '10 at 16:42 hi, I've edited my answer and added another possible solution – sled Sep 8 '10 at 17:13 Thanks but @user.books. Find(:all) errors – AnApprentice Sep 9 '10 at 6:53.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions