Active record association

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.

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 J. wrote:

t.timestamps
t.integer :group_id
for officer in officers
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 F.

http://twitter.com/erolfornoles
http://ph.linkedin.com/in/erolfornoles

Erol F. 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,
GitHub - ianwhite/nested_has_many_through: rails plugin that allows has_many :through to go through other has_many :throughs might help. I don’t
know if this works with Rails 3.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]