1

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)

tereško
  • 58,060
  • 25
  • 98
  • 150
OVERTONE
  • 11,797
  • 20
  • 71
  • 87
  • I wonder would i be worth scraping active record and Going with a Json post that's taken apart when it hits the controller? Is this good/bad? something along the lines of this http://shaktidev.wordpress.com/2010/11/04/activeresource-with-json/ – OVERTONE Jul 13 '11 at 15:08
  • Always ask your leadership question...phrase it as a "please teach me" to make sure you don't get beaten about the head and neck, but never be afraid to ask... – jaydel Jul 13 '11 at 16:21
  • @jaydel good advice but never seems to fly. General tone I get in response is 'I'm busy'. When he does sit down with myself and the other intern, its never simple which defeats the purpose. – OVERTONE Jul 13 '11 at 16:38
  • Understood. There are always people like him out there...just don't let it affect how you deal with your future technical leadership :) – jaydel Jul 13 '11 at 18:21
  • "I post it with .save" -- see the terminology mismatch? Wouldn't it be better to post it with .post? Perhaps you can utilize an alias. (I'm just trying to guess at your boss's dislike for .save) – Mark Thomas Jul 14 '11 at 01:17
  • @Mark Thomas. I think he thinks .save gives active resource direct access to the server side database whereas I know its a post request. . Making a .post method and trying to send it across its exactly what I'm trying to do now but its proving difficult. keep getting 422's. – OVERTONE Jul 14 '11 at 09:16

2 Answers2

0

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.

diedthreetimes
  • 4,086
  • 26
  • 38
  • diedthreetimes: so send a post request using whatever lib makes it easy from client to server and then turn it into active record? Then If i want to call a method, make it accessible via get or post on server side. Is that right? apologies if i misunderstood – OVERTONE Jul 13 '11 at 15:27
  • 1
    Yes. The nice thing about the RPC libraries is they should wrap this up for you. (I haven't used them personally). So much the way that `MyRecord.find(1).id` will work client side. `MyRecord.find(1).get_status` will fire off an http request and parse the results under the hood. One caveat, only the returned result is accessible though (so puts won't work). – diedthreetimes Jul 13 '11 at 15:53
  • ended up dropping active resource entirely after talking with my boss, I asked him what the reason was for using it. He explained it had simpler built in methods that make it easier to store. I explained that these methods were the .save and .find. So after that little chat, I had to write my own class.Thanks for the help though, Although rpc looks a bit difficult for someone at my level, It seems viable. – OVERTONE Jul 15 '11 at 07:15
0

You can include the result of ARecord methods in the data shipped to your AResource model:

class MyRecord < ActiveRecord::Base
  ...
  def to_xml(options={})
    super(options.merge(:methods => [ :your_method_name_here ]))
  end
  ...
end

You'll need to re-create any other methods in the AResource model that you want to use on the client. (I don't know how a puts belongs in an ARecord model, but do whatever floats your boat.)

Jeremy Weathers
  • 2,556
  • 1
  • 16
  • 24
  • get_status only exists on the servers version of the class. The puts was just to shout back to console and let me know it was working. As it stands, I've been using the active resource simply to pass data to the server. Stuff then determined by the server added extra variables onto the active record. E.g status has no business on the resource but comes in handy if I want to call it in the server side database. I don't think for what I want to do, putting all the methods on the Arsrc is a good idea but I'll look into it regardless thanks. – OVERTONE Jul 13 '11 at 15:37
  • 1
    As long as the ARecord methods are only processing the data sent from AResource, tie them to a before_validation or before_save callback and leave them in ARecord. – Jeremy Weathers Jul 13 '11 at 15:42
  • I'm sorry. I don't actually understand what you've just said. Perhaps you could briefly explain a callback to me. And The Arecords are processing data sent from the Aresource but are also storing and processing their own. I don't know if that helps. – OVERTONE Jul 13 '11 at 15:50
  • Read this: http://edgeguides.rubyonrails.org/active_record_validations_callbacks.html#callbacks-overview – Jeremy Weathers Jul 13 '11 at 16:17