I have a project model, it has many users through project_members (id, user_id, project_id, leader)
the project_members table leader column is a tinyint(1) column (true/false). How would I set it up so I could do something like the following:
@project = Project.find(id)
@project.leader (this would give me a User object of the person where the leader column is set to true in the project_members table)
It’s not an association, tried that and it’s looking to leader as an id column, not true/false.
Do I set methods (getter/setter)? Hope i’m being somewhat clear, thanks for any help.
I would create a named scope for the user (project member) model that would look something like this:
class User < ActiveRecord::Base
named_scope :team_lead, :conditions => {:leader => 1}
end
Your call would then look like:
@project = Project.find(params[:id])
@project.project_members.team_lead
More on named scopes can be found in the Rails API (http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html)
All you need to do is add a “?” to the end of the attribute name. @project.leader?
okay, i TOTALLY screwed up how I asked this. Late friday night, should know better than try to post something coherent.
Associations are the same but here’s the setup,
@project = Project.find(params[:id]) <= gets you a project object
@project.project_members <= returns an array of user objects
what I would like to have, that isn’t happening
@project.project_members.leader <= returns a single user object where leader column is set to true (tinyint(1)).
So I guess my question is, how do I add that leader method to the @project.project_members array? virtual_attribute?