user.roles -- How do you list the role names of the user?

Discussion in 'Ruby' started by Bob Sanders, Nov 9, 2007.

  1. Bob Sanders

    Bob Sanders Guest

    Hello,

    I want to list out the role names that belong to a user. I have
    user.roles as the table, so I'm interested in the "name" column on that
    roles table. If I want to separate the names using a comma [ e.g. using
    join(", ") ], how would I do it?

    For instance:

    <% for role in user.roles %><% role.name %><% end %>

    displays all of the roles of the user, but without the comma. There must
    be a better way to do it (a less tedious way), and also separate them
    with commas?

    Any idea?
    --
    Posted via http://www.ruby-forum.com/.
    Bob Sanders, Nov 9, 2007
    #1
    1. Advertising

  2. Bob Sanders

    James Britt Guest

    Bob Sanders wrote:
    > Hello,
    >
    > I want to list out the role names that belong to a user. I have
    > user.roles as the table, so I'm interested in the "name" column on that
    > roles table. If I want to separate the names using a comma [ e.g. using
    > .join(", ") ], how would I do it?
    >
    > For instance:
    >
    > <% for role in user.roles %><% role.name %><% end %>
    >
    > displays all of the roles of the user, but without the comma. There must
    > be a better way to do it (a less tedious way), and also separate them
    > with commas?
    >


    > Any idea?


    What happens when you do user.roles.join( ',' ) ?

    You may need to explain more about the object model. Is this part of an
    ORM library? Og? Sequel? ActiveRecord?





    --
    James Britt

    "Tear it up and start again."
    - Anonymous
    James Britt, Nov 9, 2007
    #2
    1. Advertising

  3. James Britt wrote:
    > Bob Sanders wrote:
    >> I want to list out the role names that belong to a user.
    >> <% for role in user.roles %><% role.name %><% end %>

    > What happens when you do user.roles.join( ',' ) ?
    > You may need to explain more about the object model. Is this part of an
    > ORM library? Og? Sequel? ActiveRecord?


    It'll be a Rails question. Please take Rails questions to the Rails list.

    The OP wants <%= user.roles.map(&:name)*',' %>

    Clifford Heath.
    Clifford Heath, Nov 9, 2007
    #3
  4. Bob Sanders

    Jacob Basham Guest

    Voila

    <%= user.roles.join(",&nbsp;") %>

    http://www.ruby-doc.org/core/classes/Array.html#M002205

    Jake

    On Nov 8, 2007, at 11:58 PM, Bob Sanders wrote:

    > Hello,
    >
    > I want to list out the role names that belong to a user. I have
    > user.roles as the table, so I'm interested in the "name" column on
    > that
    > roles table. If I want to separate the names using a comma [ e.g.
    > using
    > .join(", ") ], how would I do it?
    >
    > For instance:
    >
    > <% for role in user.roles %><% role.name %><% end %>
    >
    > displays all of the roles of the user, but without the comma. There
    > must
    > be a better way to do it (a less tedious way), and also separate them
    > with commas?
    >
    > Any idea?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    Jacob Basham, Nov 9, 2007
    #4
  5. Bob Sanders

    Bob Sanders Guest

    Re: user.roles -- How do you list the role names of the user

    Jacob Basham wrote:
    > Voila
    >
    > <%= user.roles.join(",&nbsp;") %>
    >
    > http://www.ruby-doc.org/core/classes/Array.html#M002205
    >
    > Jake


    Hi Jake, I tried that -- and ended up with a pound sign (#)

    I'm guessing that brings up an array, and I'll have to incorporate the
    "name" in there somehow

    e.g. user.roles[name].join(",&nbsp;")

    (which is probably totally wrong)

    any ideas guys?
    --
    Posted via http://www.ruby-forum.com/.
    Bob Sanders, Nov 9, 2007
    #5
  6. Bob Sanders

    Bob Sanders Guest

    Re: user.roles -- How do you list the role names of the user

    Clifford Heath wrote:
    > James Britt wrote:
    >> Bob Sanders wrote:
    >>> I want to list out the role names that belong to a user.
    >>> <% for role in user.roles %><% role.name %><% end %>

    >> What happens when you do user.roles.join( ',' ) ?
    >> You may need to explain more about the object model. Is this part of an
    >> ORM library? Og? Sequel? ActiveRecord?

    >
    > It'll be a Rails question. Please take Rails questions to the Rails
    > list.
    >
    > The OP wants <%= user.roles.map(&:name)*',' %>
    >
    > Clifford Heath.


    Clifford! Thank you, man!

    I didn't see your solution before. You got it. Thank you, thank you,
    Clifford :)
    --
    Posted via http://www.ruby-forum.com/.
    Bob Sanders, Nov 9, 2007
    #6
  7. Bob Sanders

    Jacob Basham Guest

    Re: user.roles -- How do you list the role names of the user

    Ahh yes, I missed that part!

    On Nov 9, 2007, at 12:23 AM, Bob Sanders wrote:

    > Jacob Basham wrote:
    >> Voila
    >>
    >> <%= user.roles.join(",&nbsp;") %>
    >>
    >> http://www.ruby-doc.org/core/classes/Array.html#M002205
    >>
    >> Jake

    >
    > Hi Jake, I tried that -- and ended up with a pound sign (#)
    >
    > I'm guessing that brings up an array, and I'll have to incorporate the
    > "name" in there somehow
    >
    > e.g. user.roles[name].join(",&nbsp;")
    >
    > (which is probably totally wrong)
    >
    > any ideas guys?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    Jacob Basham, Nov 10, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesper Stocholm
    Replies:
    2
    Views:
    8,104
    John Saunders
    Aug 23, 2003
  2. Liet Kynes
    Replies:
    0
    Views:
    490
    Liet Kynes
    Nov 26, 2003
  3. David
    Replies:
    5
    Views:
    482
    David
    Nov 11, 2004
  4. J
    Replies:
    0
    Views:
    998
  5. Kursat
    Replies:
    1
    Views:
    314
    Dominick Baier
    May 7, 2007
Loading...

Share This Page