Search code examples
ruby-on-railsruby-on-rails-3activerecordactiveresource

Activerecord model method passed to ActiveResource


Using activeresource, I'm pulling an activerecord model from another application. This model does a less than simple method pulling results from several associations. How do I pass the result of this method with the activeresource call. I've tried adding it to the activeresource schema, and I'm sure I could do it with a custom route, or do that method messily in the activeresource model, but I'd prefer to just have it passed with the original call to the model.

Example:

app1: activeresource model RemoteModel

app2: activerecord model MyModel

MyModel responds to the method run_calculation_on_several_associations

How do I get 'run_calculation_on_several_associations' to RemoteModel simply and directly? Preferably when RemoteModel calls out to MyModel, without running a custom method or route.


Solution

  • You would have to change the xml/json response on your MyModel controller to include the method you want to use.

    class MyModelController < ApplicationController
      def show
        @my_model.find(params[:id])
    
        respond_to do |format|
          format.html
          format.xml { render xml: @user.to_xml(:methods => [ :run_calculation_on_several_associations])}
        end
      end
    end