Thursday, 20 November 2014

ActiveResource Custom calls response on get method

In this article i am going to explain one problem on active resource custom calls on get method and what the solution on that. Lets see whats the problem while you are making any active resource get calls. Lets see one example code snippet.

User.get(:active)  # GET /users/active.json
# => [{:id => 1, :name => 'Madhan'}, {:id => 2, :name => 'Ayyasamy'}]

User.get(:active, :awesome => true)  # GET /users/active.json?awesome=true
# => [{:id => 1, :name => 'Madhan'}]

Key Point: The objects returned from the called methods will be just ordinary hash only. It will not be automatically converted into ActiveResource::Base instances.

If you are expecting ActiveResource::Base instances, you have to use "find" class methods with ":from" option. This is the solution for the problem.

Then the new method will be like this

User.find(:all,:from=> :active)  # GET /users/active.json

# => <#User...> <#User...>.... (result as ActiveResource::Base instances).
Then you can use inside the iterations user.first.login_name something like this.

Thanks for reading this article. Hope it will be useful for you.


7 comments :

  1. It was really a nice article and i was really impressed by reading this
    Ruby on Rails Online Training

    ReplyDelete
  2. Thanks for sharing this wonderful article, Its very informative. Read Also.. How to Connect My Epson Printer to a Wired Network?

    ReplyDelete
  3. I truly like to reading your post. Thank you so much for taking the time to share such a nice information Steps To Disable Avast Antivirus

    ReplyDelete
  4. Hey admin, I found some really interesting things. Thank you so much for sharing this.. Also read this.. Choose Best Tech Blogs

    ReplyDelete
  5. I truly like to reading your post. Thank you so much for taking the time to share such a nice information Here’s How You Can Use PIP Mode in Chrome, Edge, and Firefox

    ReplyDelete
  6. Hey! I was just exploring and came across your site. The articles and blogs were quite interesting to read. I am from technological field. If someone is interested to read about Antivirus software, then do visit my site
    mcafee.com/activate or
    norton.com/setup
    office.com/setup

    ReplyDelete