SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    976
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to set up true/false attribute for model

    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.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    Cleveland, OH
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All you need to do is add a "?" to the end of the attribute name. @project.leader?

  3. #3
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    976
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    Cleveland, OH
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would create a named scope for the user (project member) model that would look something like this:

    Code:
    class User < ActiveRecord::Base
      named_scope :team_lead, :conditions => {:leader => 1}
    end
    Your call would then look like:
    Code:
    @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/A...ssMethods.html)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •