I have four tables and and its migrations are like create_table :officers do |t| t.string :name t.timestamps end create_table :areas do |t| t.string :name t.integer :officer_id t.timestamps end create_table :groups do |t| t.string :name t.integer :area_id t.timestamps end create_table :group_members do |t| t.string :name t.integer :group_id t.timestamps end table releationships like officer has many areas, area has many groups and group has many group_members. currently i have used following way to access group_members from officer, like officers = Officer.all for officer in officers areas = Area.all(:conditions=>["officer_id ?",officer.id],:order=>:name) area_ids = areas.map { |area| area.id } area_id = area_ids*"," if area_ids.length > 0 groups = Group.all(:conditions=>["area_id in #{area_id}"],:order=>:name) group_ids = groups.map { |group| group.id } group_id = group_ids*"," if group_ids.length > 0 group_members = GroupMember.all(:conditions=>["group_id in #{group_id} and name like "Mrs.%%"],:order=>:name) for group_member in group_members member_name = group_member.name end end Is it possible to directly access the group_members from officer model using rails active_record association that means officer.group_members or tell me easiest way.Give me the example with such relationship. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
The functionality you''re asking for is a nested has_many :through, which isn''t done yet: https://rails.lighthouseapp.com/projects/8994/tickets/1152-support-for-nested-has_many-through-associations On 10/13/10 9:44 PM, Manivannan Jeganathan wrote:> I have four tables and and its migrations are like > > create_table :officers do |t| > t.string :name > t.timestamps > end > > create_table :areas do |t| > t.string :name > t.integer :officer_id > t.timestamps > end > > create_table :groups do |t| > t.string :name > t.integer :area_id > t.timestamps > end > > create_table :group_members do |t| > t.string :name > t.integer :group_id > t.timestamps > end > > table releationships like officer has many areas, area has many groups > and group has many group_members. > > currently i have used following way to access group_members from > officer, like > > officers = Officer.all > for officer in officers > areas = Area.all(:conditions=>["officer_id > ?",officer.id],:order=>:name) > area_ids = areas.map { |area| area.id } > area_id = area_ids*"," if area_ids.length > 0 > > groups = Group.all(:conditions=>["area_id in > #{area_id}"],:order=>:name) > group_ids = groups.map { |group| group.id } > group_id = group_ids*"," if group_ids.length > 0 > > group_members = GroupMember.all(:conditions=>["group_id in #{group_id} > and name like "Mrs.%%"],:order=>:name) > for group_member in group_members > member_name = group_member.name > end > end > > Is it possible to directly access the group_members from officer model > using rails active_record association that means officer.group_members > or tell me easiest way.Give me the example with such relationship. >-- Erol Fornoles http://github.com/Erol http://twitter.com/erolfornoles http://ph.linkedin.com/in/erolfornoles -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Erol Fornoles wrote in post #949877:> The functionality you''re asking for is a nested has_many :through, which > isn''t done yet: > >https://rails.lighthouseapp.com/projects/8994/tickets/1152-support-for-nested-has_many-through-associations If you''re using Rails 2.3, http://github.com/ianwhite/nested_has_many_through might help. I don''t know if this works with Rails 3. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.