开发者

Rails, Outputing a List of Users & Specifying if they are permissioned or not

Here's what I have today: I output a list of @users

What I need to do is output a list of users and say if they are assigned to the current project.

I could loop through each user and check in the database, but that would be a lot of data base hits, an additional 10 hits for displaying 10 users.

My question to you, is there a smart, more efficient, rails way to do this? Ideas: Perhaps I load a list of all of a project's current users, and check against that array when outputting through the user's loop? Or maybe some other method?

Interested in hear your suggestions and 开发者_高级运维if you don't mind a small code snippet to get me started.

Thank you!

UPDATE - adding models

User model
belongs_to :instance
has_many :permissions
has_many :projects, :through => :permissions

Permissions Model:
belongs_to :user 
belongs_to :project 
belongs_to :role

Project Model:
has_many :permissions
has_many :users, :through => :permissions

The query I'm using currently is:

@users = find(:all, :joins => :instance, :select => 'users.*, instances.domain', :conditions => ['fname LIKE ? or lname LIKE ?', "%#{search}%", "%#{search}%"])

What I want to know is, for the user being displayed. Are they currently a member of the project_id in the URL? Or if not, show an ADD Member button.


Use :include (Just check this 4.1.2.7 :include )

 User.all( :include =>:projects )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜