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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yan Oleg 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Mitchell On Fri, Nov 21, 2008 at 8:59 AM, Yan Oleg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Yan Oleg 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! > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
James Mitchell 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 Mitchell > > > > On Fri, Nov 21, 2008 at 8:59 AM, Yan OlegNO, 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Mitchell On Fri, Nov 21, 2008 at 9:51 AM, Yan Oleg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > James Mitchell 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 Mitchell >> >> >> >> On Fri, Nov 21, 2008 at 8:59 AM, Yan Oleg > > 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 > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Nov 21, 1:25 pm, "James Mitchell" <jmitc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 Mitchell > > On Fri, Nov 21, 2008 at 9:51 AM, Yan Oleg > > <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > James Mitchell 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 Mitchell > > >> On Fri, Nov 21, 2008 at 8:59 AM, Yan Oleg > > > 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 > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
"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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks to Harold. I used uniq method and it worksfine!! :) Thank you! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---