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.