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.


Saturday 8 November 2014

ActiveResource Post custom method calls in rails

In this article i am going to explain work around/fix for "wrong status line: "" issue while you are doing any activeresource stuff in rails.

What is the problem,why "wrong status line: "" while making resource calls on "post" verb?

Here is my code snippet where is the line making the problem.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class FactoringCashReceipt < ActiveResource::Base
  self.site = configatron.cync.factoring.url
  self.element_name = "cash_receipts"
  #self.prefix = "/borrowers/:borrower_id/"
  self.headers['X-BACK-KEY'] = BACKGROUND_SECRET_KEY

  def self.create_bulk_cash_receipts(batch,cash_receipts_objects)
    response = post("do_bulk_insert",{cash_receipts: cash_receipts_objects.to_json})
  end
  #def self.by_borrower(borrower_id)
  #  borrower_receivables = get("get_all_customers_by_borrower", {borrower_id: borrower_id})
  #end
  #
  #def self.by_invoice_no_lookup(lookup_no)
  #  customers = get("get_all_customers_by_borrower", {borrower_id: borrower_id})
  #end
  #
  #def self.by_borrower_with_lookup(borrower_id,lookup_no)
  #
  #end
  #
  #def self.by_borrower_with_customer_with_lookup(borrower_id,customer_id,lookup_no)
  #  receivable = find(:all, from: :get_particular_receivable, params: {borrower_id: borrower_id,customer_id: customer_id,receivable_no: lookup_no})
  #end
  #
  #def self.get_corresponding_receivables(params)
  #  receivables = find(:all, from: :get_corresponding_receivables, params: {borrower_id: params[:borrower_id],customer_id: params[:customer_id],receivable_no: params[:receivable_no]})
  #end

end

Problem on line no 8
response = post("do_bulk_insert",{cash_receipts: cash_receipts_objects.to_json})
The above line does not make a resource call to other engine. This line complaining that "URI is too long".
Now there is a question on your mind, what other options available to send large data on post call while activeresource. The answer is below

Proper Syntax  for post call is 

post(your_custom_method_name,options = {},body='')
so what mistake i did was, i passed all varibables inside the options which is passed as query parameter.

response = post("do_bulk_insert",{cash_receipts: cash_receipts_objects.to_json}) (Wrong Approach)
after modification the code should like this
Either
response = post("do_bulk_insert",{},{cash_receipts: cash_receipts_objects}.to_json) (Right Approach)
or
response = post("do_bulk_insert",nil,{cash_receipts: cash_receipts_objects}.to_json)
(Right Approach).

Hopefully now you can understand how post custom method calls on ActiveResource working and what common mistakes we do on that.

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