I have some ActiveResource models like so:
class Meeting
has_many :sections
end
class Section
has_many :items
end
class Items
has_many :items
end
When I do a find on a Meeting, I get a json response like:
{
id: 1, ...
sections: [
{
id: 1, ...
items: [
{id: 16, ...
items: [
{id: 534, ...
items: [
{id: 8976, ...},
{id: 8977, ...}
]}
]}
]
}
}
And run the following (heavily edited) routine:
@meeting = Meeting.find(@meeting_guid)
import_sections
private
def import_sections
@meeting.sections.each do |section|
new_section = Item.find_or_initialize_by(foreign_guid: section.guid)
new_section.update!(layout: :section)
import_items(section, new_section)
end
end
def import_items(item, parent)
item.items.each do |item|
position = mema_item.orderpreference.to_i + 1
sub_item = Item.find_or_initialize_by(foreign_guid: item.guid)
sub_item.update!(layout: :item, title: item.name, parent: parent)
import_items(item, sub_item)
end
end
When it hits Items 8976 and 8977, it makes requests to /items?item_id=8976 and /items?item_id=8977. Is there a way to prevent it from making these requests when it hits the end of the last child? I don't think I'll ever call Items directly, so can I disable requests on the Item model or something?
I feel silly for not catching this, but I was able to solve the problem quite simply, albeit not through an ActiveResource feature. On the recursive import_items call, I check for
if item.attributes.key? 'items', essentially just checking the attributes on the parent object without going into the relationship and creating an api call.