Forum: Ruby on Rails Distinct selection

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Yan O. (Guest)
on 2008-11-21 09:46
I have the following code:

members.select {|m| m.role.assignable?}.collect {|m| m.role}.sort

There are two tables: "roles" and "members". One role record may have
many foreign member records.

But in this case I get many same roles, I want to apply "distinct"
operation to make roles unique in results. How can I do this?
Yan O. (Guest)
on 2008-11-21 15:59
Yan O. wrote:
> I have the following code:
>
> members.select {|m| m.role.assignable?}.collect {|m| m.role}.sort
>
> There are two tables: "roles" and "members". One role record may have
> many foreign member records.
>
> But in this case I get many same roles, I want to apply "distinct"
> operation to make roles unique in results. How can I do this?

Please help me!
James M. (Guest)
on 2008-11-21 16:14
(Received via mailing list)
What exactly are you trying to do?

From reading what you wrote, it sounds like you want a list of roles
that have not already been assigned.  Is that right?

--
James M.



On Fri, Nov 21, 2008 at 8:59 AM, Yan O.
Yan O. (Guest)
on 2008-11-21 16:51
James M. wrote:
> What exactly are you trying to do?
>
> From reading what you wrote, it sounds like you want a list of roles
> that have not already been assigned.  Is that right?
>
> --
> James M.
>
>
>
> On Fri, Nov 21, 2008 at 8:59 AM, Yan O.

NO, it isn't right.

If I use this code:

members.select {|m| m.role.assignable?}.collect {|m| m.role}.sort

...I get several roles with the same ID and NAME, but I want to get
unique ones, like using DISTINCT operation in SQL statements (SELECT
DISTINCT name FROM roles.....)

But unfortunately I can get the list from table "members", where records
may have several same role names:

Manager
Tester
Manager
Manager
Manager
Tester
Developer
Developer
Tester
Developer
Tester
Developer
Developer
Developer

But I have to get this list:

Manager
Tester
Developer
James M. (Guest)
on 2008-11-21 20:33
(Received via mailing list)
Ok, maybe we misunderstood each other.

If you want a list of roles, why would you go through the members
table to get it?  You can just do Roles.find(:all).map(&:name)

I'm not sure what you doing with "assignable?".  It seems to be
important to you, but you have not explained what it is supposed to
return.

What does it do?  Does it return:
  a) members who do not have a role and are therefore able to be
assigned a role ?
  b) some awkward pass-through delegation to roles that aren't assigned
yet ?


--
James M.



On Fri, Nov 21, 2008 at 9:51 AM, Yan O.
Harold (Guest)
on 2008-11-21 23:54
(Received via mailing list)
Agreed with James that we don't have much info on what you are trying
to achieve. Why can't you just query the roles table directly?

Anyways, you can always uniq the array:
http://www.ruby-doc.org/core/classes/Array.html#M002237
Yan O. (Guest)
on 2008-11-25 12:39
"assignable?" is just a filter to select roles that have attribute
"assignable" set to true.

Regarding roles... I want to select not all roles, but roles that are
members of project. And also I cannot change database structure. So I
have the following entities relations:

Roles <- Members -> Projects
          |
         \ /
         Users


Unfortunately I don't know how to select (using RoR) unique roles that
regard only defined project.

In SQL it looks like:

SELECT Roles.id, Roles.name
FROM Roles, Members
WHERE Members.project_id=5
Yan O. (Guest)
on 2008-11-25 15:14
Thanks to Harold. I used uniq method and it worksfine!! :)
Thank you!
This topic is locked and can not be replied to.