ActiveResource collection new problem

ActiveResource can be pretty helpful when you have a RESTful JSON API. Although it has some limitations. One of the most irritating is a lack of nested resources new scope method. When you have structure like this:

But it just doesn’t seem right (well at least after working with ActiveRecord). ActiveResource::Collection doesn’t support building resources through it.

alias_method and a bit of magic as a solution

To obtain such a behaviour we have to:

save original relation method using alias_method

create a module that will contain our extension that will allow us to build new resources directly

define relation method that will mix the module with original relation method output

return mixed relation output

Once we have all of this, we will be able to just:

user = User.last
new_stat = user.daily_stats.new
new_stat.save!

overwriting method without losing the original one

Ok, so we have our relation daily_stats method that will return us a given ActiveResource::Collection. We will have to overwrite it, but we can’t lose the original one. To obtain this, we can use alias_method:

class User < ActiveResource::Base
self.site = 'your_api_end_point'
has_many :daily_stats
alias_method :native_daily_stats, :daily_stats
def daily_stats
# Now we can do whatever we want here, because we can get
# the ActiveResource::Collection of DailyStat via
# native_daily_stats method
# Something fancy will happen here...
# and after that we will just return native_daily_stats
native_daily_stats
end
end

It’s worth pointing out, that you can use this trick to redefine/change method without losing the original one. Especially when you can’t use super (because it’s not an inherited one, etc).

Extension module for our new daily_stats

Now we have to create our extension that will be used to modify the ActiveResource::Collection (but only in a daily_stats scope):