Rails XML - Updating server with multiple records - ruby-on-rails

I'm using a Rails 2.x server, and trying to handle all interaction using XML. I've got no problem with updating the database with single XML records (creating/updating/deleting), but I've got a group of new records I'd like to add in one transaction, and I can't get it to work.
The XML I'm submitting looks like this:
<invitees>
<invitee>
<event>32</event>
<name>Jack</name>
</invitee>
<invitee>
<event>42</event>
<name>Alan</name>
</invitee>
</invitees>
I'm new to Rails; I can handle the basic Ruby/Rails stuff, but handling this is clearly beyond me! The code to handle this is failing, and I don't understand why. This is what I've got:
#invitees = params[:invitees]
for #invitee in #invitees
thisinvitee = Invitee.new(#invitee)
thisinvitee.save
end
This fails in "Invitee.new", with error:
NoMethodError (undefined method `stringify_keys!' for #):
Can anyone give me an idea what I'm doing wrong, and how to handle multiple XML records being submitted in a single transaction from a remote client?
Thanks for any help/pointers.

Related

Rails ActiveRecord method save! saves data but there is no record in db

I am a very confused. I have code which stores data in the database succesfully, then I start a Sidekiq job, and first line is to select that record, but it fails on error RecordNotFound (Couldn't find Message with 'id'=5789035)
Here is the code:
message = Message.new(
user: user,
from: from,
content: content,
kind: kind
)
message.save!
Until now, everything is absolutely ok, save returns object with ID, but then I kick out the job.
SendMessage.perform_later(message_id: message.id) if message
The code of SendMessage fails on the first line, which is message = Message.find(args.first[:message_id])
And fails on error RecordNotFound (Couldn't find Message with 'id'=5789035) and there is no record in the database with this id.
I don't understand why method save didn't fail or so, or why the record is missing. It happens just sometimes, I cannot find the case. But yes can be anything, but why method save behaves like this?
I am logging message object after calling save! method with ID, and there is data including ID inside.
When I that case repeat on console, it is successfully repeated.
These failures are around 10 - 50 per month and successful savings are around 2 thousand.
The database is set correctly
Please do you have any suggestions?
It could be caused by a cached DB which is a bit common in ActiveJobs
try:
Message.uncached do
message = Message.find(args.first[:message_id])
# rest of the block
end
alternately ActiveJob now uses the new Globalid library behind the scenes to serialize/deserialize an ActiveRecord instance, therefore you can now pass an ActiveRecord object.
SendMessage.perform_later(message: message) if message

Why Generated Active Record Query dosen't run in sidekiq

I am having a code that runs a delayed job that generates a report and then sends in an email here :
InstancesExportJob.perform_later(instances: instances, custom_field_key: options[:custom_field_key],tag_columns: options[:tags],user: User.current,report_url: report_url)
where instances is a generated active record query that is generated by another class this code fails and gives the following error: ActiveJob::SerializationError:Unsupported argument type: ActiveRecord::Relation.
where the first operation that is done on instances is a map function call.
but changing the code to this makes it work fine:
InstancesExportJob.perform_later(instances: instances.to_a, custom_field_key: options[:custom_field_key],tag_columns: options[:tags],user: User.current,report_url: report_url).
I am confused as running the code without delayed jobs works fine.
Am using rails 4.2 and sidekiq
I think the problem was with sidekiq as it do not allow a wide variety of parameter types and having the instance as an active record relation made the whole thing raise an error

Active resource find doesn't work in production

As the title says, I have a simple ActiveResource in my application that is supposed to get data from an api. The collection works perfectly both locally and on the production server. However, .find doesn't work in production, i get a weird error:
MyResource.find(1, params: { website_id: 2 })
ArgumentError: wrong number of arguments (given 2, expected 0..1)
The same query works if I run it from local console connected to the api. API returns a valid response. I have no idea how else to debug it.
class MyResource < ActiveResource::Base
self.site = Rails.configuration.content_url
self.prefix = "/api/websites/:website_id/"
self.element_name = "game"
end
I have to say, i have other resources in the application using the same api, find method works for them, only this one has issues.
for some reason it makes a call to
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/core.rb#L330
with 2 arguments, but i can't figure out who makes such call. And it only happens in production mode, not in dev.
I found the problem, but it's the weirdest thing ever.
In my show method in the api controller i was returning an object but i was merging the hash with some extra data. If i opened in the browser, the response was fine, development worked, but for some reason, in production it doesn't want to accept the merge.
So problem solved, but still have no idea why it's happening like that.
I ran into a similar issue in an app that used ActiveRecord for a vendor model and ActiveResource for a delivery model that has a vendor attribute. In development, if the vendor model has not yet been loaded, you can fetch a delivery and deserialize it, including the vendor attribute. If, however, the vendor model has been loaded already, attempting to fetch a delivery leads to the error described in this issue.
How to reproduce in a console:
Vendor.connection
Delivery.find(1)
Seems that AResource is trying to create a new instance of Vendor.

Complex Rails app with new ActiveRecord::RecordNotFound error

I have adopted a Rails app that has a lot of complex relationships. We started getting ActiveRecord::RecordNotFound error and am trying to track it down.
Is there a way to wrap relationships in a begin ... rescue block to determine which one is causing us problems similar to what they are doing here for a find method? Or is there a way to log that exact SQL call that is getting the RecordNotFound error?
Edit
I am not able to find post I was referencing but I really just need to find the relationship that is busted. In my logs, I just see that it is rendering the template for 'not_found' records but I'm not sure what is causing it.
Without your relationships & logs, everything is conjecture; but if you're getting a RecordNotFound error - it basically means you're trying to load a record which doesn't exist in your db
Is there a way to wrap relationships in a begin ... rescue block to
determing which one is causing us problems similar to what they are
doing here for a find method
No - a better way to debug is to find the logs in your /log/development.log file & then display the response here.
A pointer is that I don't think the relationship will be the issue. The relationship will just return null if nothing is there; the RecordNotFound error will be a result of ActiveRecord not being able to find the requested resource
EG
#post = Post.find 13 #-> RecordNotFound if a post with id 13 does not exist
If you post your logs & code, it will be the best way to help us solve your issue!

How to parse response from Recurly ruby client

I am trying to work with Recurly ruby client gem but getting frustrated on how to pass its response.
I'm doing a simple call to get a list of all my current plans ie
def plans
#result_object = Recurly::Plan.all
end
I believe its sending me a list of plans as an array as if I inspect the response I'm getting a bunch data ie. http://pastie.org/3086492
But if so how are you meant to parse the setup_fee_in_cents: when I am getting
setup_fee_in_cents: #<Recurly::Money USD: 0_00>
I have tried to convert the response to_hash but getting an error undefined method 'to_hash'
Has anyone used the recurly gem before or can shed some light on how I should parse the response.
Hope someone can advise.
I've never used the gem before but it appears as though it's returning you an object of type Recurly::Money. I would try reading the documentation on the Recurly::Money class to get a better idea of how to use it.
to_hash works fine on Recurly::Money object. Try updating your gem. I am using recurly 2.0.9.
r[0].unit_amount_in_cents.to_hash[:USD] gave me the price in cents.

Resources