Update an array of object using CouchRest, Rails - ruby-on-rails

Im trying to update a parameter in a collection of objects using Rails, CouchRest.
This is what im doing currently,
class User
property :country
property :some_flag
end
#users = User.by_country(:key => 'country-name')
#users.each do |user|
user.update_attributes({:some_flag => 'true'})
end
Whenever update_attributes failed for a User object i want the entire transaction to be rolled back. How can i achieve this?
Am Using CouchDB. Rails, CouchRest and Not using ActiveRecord.

CouchDB doesn't explicitly support transaction, but there is something called "bulk update" which may be of help:
http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API#Modify_Multiple_Documents_With_a_Single_Request
Example data for the update:
{
"all_or_nothing": true,
"docs": [
{"_id": "0", "_rev": "1-62657917", "integer": 10, "string": "10"},
{"_id": "1", "_rev": "2-1579510027", "integer": 2, "string": "2"},
{"_id": "2", "_rev": "2-3978456339", "integer": 3, "string": "3"}
]
}

Related

Rails flexirest model is deconstructing array of 2 objects into an object with 2 properties with an array each

I have this weird error where my flexirest model is being called with
{
"people" => [
{"name" => "Jane", "age" => 33},
{"name" => "John", "age" => 36}
]
}
When the receiving end (node) gets
{
"people": {
"name": ["Jane", "John"],
"age": [33, 36]
}
}
I know it's not the receiving service, because a postman request shows the correct structure. Any idea what might have gone wrong?
Thanks!
Showed this to an experienced senior engineer, he said it was most likely flexirest sending the data as form data.
Added request_body_type :json and now it's sending the correct information.

Matching expressions for Twilio TaskRouter workers on data in objects in arrays

I've got Twilio Taskrouter workers with attributes that look like as follows:
{
"name": "Bob",
"id": "45",
"roles": [
{ "id": "19", "name": "Foobar" },
{ "id": "20", "name": "Foobaz" }
]
}
I'd like to write a queue expression to only match Workers with roles with an id of 20. How would I do that?
It would look something like...
"20" in roles.id
...but this doesn't work. As it seems Taskrouter is not smart enough to "unroll" the ids and match within them (like using a tool like jq). I am not able to find a solution in the Twilio Taskrouter expression docs.
Twilio developer evangelist here.
I can't find a solution for you with the data like that. A workaround I just considered would be to add an array of, for example, role_ids to your worker as well. You can keep the existing array of roles, but add a simpler data type to use in the expression matching.
So, the attributes would look like this:
{
"name": "Bob",
"id": "45",
"roles": [
{ "id": "19", "name": "Foobar" },
{ "id": "20", "name": "Foobaz" }
],
"role_ids": ["19", "20"]
}
And you could then use the expression:
"20" in role_ids

Rails API Mode JBuilder Pagination

I am currently trying to work on a large data set with over 9000 records coming in and would like to paginate it. I have never worked with pagination I know the gems Kaminari and Pagy can do this. I believe I need to wrap my find with either Kaminari supplied methods or Pagy but I am not sure what I am supposed to do with my Jbuilder views after that. Below is a snippet of my find method and a builder for it. The attendees hash has roughly 9000 records causing the load to either time out or take about 5 minutes. I am hoping pagination should smooth this out.
Controller
class ConferencesController < ::ApplicationController
def attendees
#conference = Conference.find(attendee_params[:conference_id])
end
private
def attendee_params
params.permit(:conference_id)
end
end
end
View
json.full_name attendee.full_name
json.email attendee.email
json.requires_certification attendee.requires_certification
Output
{
"conference": {
"title": "Avengers Assemble",
"description": null,
"starts_at": "2021-04-21T16:00:00.0000+0000",
"ends_at": "2021-04-21T16:45:00.0000+0000",
"attendees_count": 9002
},
"attendees": [
{
"full_name": "Steve Rogers",
"email": "blank#blank.com",
"requires_certification": true
},
{
"full_name": "Bruce Banner",
"email": "denise.parisian#kirlin.biz",
"requires_certification": false
},
{
"full_name": "THor Odinson",
"email": "blank#blank.com",
"requires_certification": false
}

Rails 3: Wrapping as_json response with additional lines

I was very happy to learn of as_json to make my code DRY. And I've added the following to a model:
class ProductType < ActiveRecord::Base
has_many :component_types
def as_json(parameter)
{:name => self.name,
:description => self.description,
:children => self.componentTypes}
end
end
This is great. The only thing is that for my client side application, I need to wrap the response I get into this format, (where "items" contains what is created by as_json):
{
"identifier": "name",
"label": "name",
"items":
[
{
"name": "myName1",
"description": "myDesc1",
"children":[]
},
{
"name": "myName2",
"description": "myDesc2",
"children":[]
}
]
}
There are a lot of limitations to overriding as_json, and your issue is one of them. I'd suggest having a look at the RABL gem as I think it will help you reach your goal.

Build json from ValueObjects in Rails 3?

i was wondering how i can build a json response from a value object?
Situation?
I want to return clear json, only the field i need in the front-end. Which means: All associations should be included in the json. But again: only the fields I need. This i why i would like to use a special value object (defining the field) on top of my models.
Problem?
Is this a good idea? How to build value objects (VOs) with rails?
Thanks for helping
Of course it's possible. Look here:
Here is an example:
konata.to_json(:only => [ :id, :name ])
# => {"id": 1, "name": "Konata Izumi"}
As you're talking about associations:
konata.to_json(:include => :posts)
# => {"id": 1, "name": "Konata Izumi", "age": 16,
"created_at": "2006/08/01", "awesome": true,
"posts": [{"id": 1, "author_id": 1, "title": "Welcome to the weblog"},
{"id": 2, author_id: 1, "title": "So I was thinking"}]}
This one sounds interesting:
http://fabrik42.github.com/acts_as_api/

Resources