I''ve got a user model and a policy model. I''ve got the association set up so that both use the has_many :through syntax. Given the scenario in editing a policy, a couple of users are already assigned. How can I figure out what users are not assigned to that policy already? Thanks -- Marlon --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Marlon Moyer wrote:> I''ve got a user model and a policy model. I''ve got the association > set up so that both use the has_many :through syntax. Given the > scenario in editing a policy, a couple of users are already assigned. > How can I figure out what users are not assigned to that policy > already? Thanks > > > > -- > MarlonLet''s say you have your policy object in @policy... @unassigned_users = User.find(:all).collect {|u| u unless @policy.users.include?(u)} This line should populate @unassigned_users as an array of User objects which are not currently linked to @policy. How it works (in case you want to know)... 1.The .collect method steps through a collection, executes the associated block of code for each item in the collection, and returns an array of all the values returned by the block. The |u| in the block maps to each user object returned by User.find(:all) 2.-TV8Su+kIMv25BTGaoZj8sQ@public.gmane.org returns the collection of User objects linked to @policy. 3.The .include? method returns true if the value in the parentheses (in this case a User object) exists in the collection on which you are executing the method (in this case, @policy.users). For our purposes here, if it returns true, the block does nothing, otherwise the block returns the User object, which ends up in the @unassigned_users array. c. -- 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 -~----------~----~----~----~------~----~------~--~---
excellent.....it never fails to amaze me how powerful ruby/rails really is. On 11/9/06, Cayce Balara <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Marlon Moyer wrote: > > I''ve got a user model and a policy model. I''ve got the association > > set up so that both use the has_many :through syntax. Given the > > scenario in editing a policy, a couple of users are already assigned. > > How can I figure out what users are not assigned to that policy > > already? Thanks > > > > > > > > -- > > Marlon > > Let''s say you have your policy object in @policy... > > @unassigned_users = User.find(:all).collect {|u| u unless > @policy.users.include?(u)} > > This line should populate @unassigned_users as an array of User objects > which are not currently linked to @policy. > > How it works (in case you want to know)... > 1.The .collect method steps through a collection, executes the > associated block of code for each item in the collection, and returns an > array of all the values returned by the block. The |u| in the block maps > to each user object returned by User.find(:all) > > 2.-TV8Su+kIMv25BTGaoZj8sQ@public.gmane.org returns the collection of User objects linked to > @policy. > > 3.The .include? method returns true if the value in the parentheses (in > this case a User object) exists in the collection on which you are > executing the method (in this case, @policy.users). For our purposes > here, if it returns true, the block does nothing, otherwise the block > returns the User object, which ends up in the @unassigned_users array. > > c. > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Marlon --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yeah, block programming (something I''d never heard of before Ruby/Rails) is pure gold. c. -- 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 -~----------~----~----~----~------~----~------~--~---