Distinct selection

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. 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!

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.

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

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.

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

“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

Thanks to Harold. I used uniq method and it worksfine!! :slight_smile:
Thank you!