Search code examples
ruby-on-railsrubyrails-activerecordactiveresource

Calling active record method from active resource (Ruby/Rails)


Apologies for the very broadly title question.

Basically this follows on from my earlier question about defs and how they are called on instantiated methods.

Basically the way I have it now:

I set up an active resource on client side and post it with .save This then goes through my controller on server and stores an active record of the same class.

so MyResource-->save-->MyRecord

The MyRecord is stored with a status column containing a simple string. Thing is MyRecord class has a def called

  def get_status
    puts status
  end #Amazing method, I know

In my mind If i wanted to execute the get_status on MyRecord, all I had to do was this.

(Please note this is client side)
@test = MyRecord.find(1)
@test.get_status

Sadly this is not the case as @test becomes an active resource and cant call a method it doesnt have. (NOTE: My classes are not actually called MyRecord and MyResource, They are just title that for simplicity as I'd rather understand the solution than have someone solve it for me.)

Would anyone care to point me in the right direction to explain how I call the active record method from client side. Have I gone completely the wrong way about it and should it be processed in controller instead of model?


On a side note: Are there any alternatives to .save? My boss doesn't like it for reasons I cannot understand. (NOTE: he's a lead, I'm an intern therefore I don't argue or ask questions that seem like a challenge)


Solution

  • Honestly, ActiveResource needs a little work... I had to implement this exact feature where I work and ended up rolling my own semi RPC lib using basic http to share code between client and server.

    Really though something like ruby-rpc or DRb would probably be a better option.