1

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.

1 Answer 1

1

Use :include (Just check this 4.1.2.7 :include )

 User.all( :include =>:projects )
4
  • Thanks, not sure what (4.1.2.7 :include) is? Also, the thing is I want to display a list of users and say if they are in the project or note (based on a permissions table)... I'm not trying to show just those in the project. But a long list of user's and if they are or not a team member.
    – WozPoz
    Oct 17, 2010 at 18:40
  • 1
    Just check the link i provided and article 4.1.2.7 :include. Can u provide some code of your models.
    – Mohit Jain
    Oct 17, 2010 at 18:57
  • Still trying to grasp this. Very interesting, hasn't clicked yet. The docs I read for "4.1.2.7 :include" talk about using include in the model not the query. Which are you proposing? Code of models added above. Thoughts?
    – WozPoz
    Oct 18, 2010 at 2:54
  • I tried something like this when I output every user. But this hits the database everytime. It also errord. thoughts? "<%= Permission.where(:user_id=> searchresult.id ).where(:project_id=> @project.id).first %>"
    – WozPoz
    Oct 18, 2010 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.