danlunde
2008-May-01 16:42 UTC
Iterating over has_many objects with the :through association together
It is often the case when I iterate over an array of objects in a has_many_through association that I want access to both the :has_many objects and the :through objects. There is generally something in that join object that is useful to get at, but enumerating with the each method, I don''t have easy access to that join object. Example: class User < ActiveRecord::Base has_many :memberships has_many :groups, :through => :memberships end # ugly way user.groups.each do |group| membership = user.memberships.detect { |memberships| memberships.group_id == group.id} # or membership = user.memberships.find(:first, :conditions => ["memberships.group_id = ?", group.id]} puts "#{group.name}, #{membership.status}" end # a better way user.groups.each_with_join do |group, membership| puts "#{group.name}, #{membership.status}" end I created a ticket with a patch (tested) in lighthouse: http://rails.lighthouseapp.com/projects/8994/tickets/82-iterate-over-has_many-array-with-the-join-through-model Let me know what you think! Daniel --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Steven Soroka
2008-May-01 16:50 UTC
(for 2.1) has_many :through .create with no parameters causes a "can''t dup NilClass" error
I was hoping this ticket could be resolved in time for 2.1, it''s complete with tests that show the failure. http://dev.rubyonrails.org/ticket/11565 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
danlunde
2008-May-01 16:59 UTC
Iterating over has_many objects with the :through association together
But your ticket has nothing to do with the one I was proposing, so let''s not lump them together. On May 1, 12:50 pm, Steven Soroka <ssorok...@gmail.com> wrote:> I was hoping this ticket could be resolved in time for 2.1, it''s > complete with tests that show the failure. > > http://dev.rubyonrails.org/ticket/11565--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Steven Soroka
2008-May-01 17:19 UTC
Re: Iterating over has_many objects with the :through association together
Sorry about that. Wasn''t my intent. On 1-May-08, at 11:59 AM, danlunde wrote:> > But your ticket has nothing to do with the one I was proposing, so > let''s not lump them together. > > On May 1, 12:50 pm, Steven Soroka <ssorok...@gmail.com> wrote: >> I was hoping this ticket could be resolved in time for 2.1, it''s >> complete with tests that show the failure. >> >> http://dev.rubyonrails.org/ticket/11565 > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Koziarski
2008-May-01 20:09 UTC
Re: Iterating over has_many objects with the :through association together
On Fri, May 2, 2008 at 4:42 AM, danlunde <danlunde@gmail.com> wrote:> > It is often the case when I iterate over an array of objects in a > has_many_through association that I want access to both the :has_many > objects and the :through objects. There is generally something in > that join object that is useful to get at, but enumerating with the > each method, I don''t have easy access to that join object. > > Example: > > class User < ActiveRecord::Base > has_many :memberships > has_many :groups, :through => :memberships > end > > # ugly way > user.groups.each do |group| > membership = user.memberships.detect { |memberships| > memberships.group_id == group.id} > # or > membership = user.memberships.find(:first, :conditions => > ["memberships.group_id = ?", group.id]} > puts "#{group.name}, #{membership.status}" > end > > # a better way > user.groups.each_with_join do |group, membership| > puts "#{group.name}, #{membership.status}" > endThis should really just be: user.memberships.each do |membership| puts membership.status, membership.group.name end So I can''t the need for this feature.> > I created a ticket with a patch (tested) in lighthouse: > http://rails.lighthouseapp.com/projects/8994/tickets/82-iterate-over-has_many-array-with-the-join-through-model > > Let me know what you think! > > Daniel > > >-- Cheers Koz --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---