Assign value from api response to specific model object in RAILS - ruby-on-rails

I have a Contact model that has three attributes:
name, email, id
name and email are both present, but id is null.
I am using name and email to pass in through an API Post call, and this is the response I get back:
{"count": 1, "next": null, "previous": null, "results": [
{"id": 8067950, "name": "Bill","email": "test#test.com"}]}
My loop looks like this:
contacts_array.each do |contact|
api_call(contact.name, contact.email)
end
contacts_array is an array of ruby objects, namely Contact models. How can I, within that loop, assign the response id I get back to the contact used in the api_call?

I didn't really understand the difference between your Person and Contact model, but I'll assume they're the same model.
Generally you should know that it's a bad idea to handle the field called id on your own in rails. If you insist on storing the id which is returned from the call, then you should store it in another field, like api_id for example.
Below is a code that should work fine if api_call returns a Ruby Hash like the one in your question.
contacts_array.each do |contact|
contact.api_id = api_call(contact.name, contact.email)['results'].first['id']
end

Related

How do I get a group count with multiple columns in the group with a virtual attribute

Keeping is simple assume I have a model "Loan" that has 2 attributes "Type" and "Status". Type as possible values of "Home", "Auto", "Building" and Status has possible values of "Open", "Pending", "Closed" and nil.
Without manually writing the SQL (using group and count?) how do I get the results of:
select type, status, count(*) from loans group by 1, 2;
I could do
Load.group(:type, :status).count()
But there are situations where status might be nil. In that case I want to replace the nil with "Unknown"
I tried adding a virtual attribute to the model:
def usable_status
status.nil? ? 'Unknown' : status
end
So I could then do
Load.group(:type, :usable_status).count()
But count didn't seem to recognize the virtual attribute since it is trying to pass it directly to the database.
Ideas?
Well, that's pretty easy to achieve by combining the rails styling with the SQL functionality:
Loan.group(:type, "COALESCE(status, 'Unknown')").count()

Create SharePoint list item with lookup field with Microsoft Graph

I am trying to create list items with Microsoft Graph's new SharePoint endpoint. My URL is as follows:
https://graph.microsoft.com/beta/sites/{site-id}/lists/{list-id}/items
Calling this URL with POST, and a body like this:
{
"fields": {
"Title": "test",
}
}
.. works! But if I include a lookup field, the lookup field is always empty. I have tried with
"{columnName}": "id",
"{columnName}": "id;#value",
"{columnName}": {
"#odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference", //and others like this
"Id": "id",
"Value": "value",
}
"{columnName}": "value",
"{columnName}Id": "id",
None of these seems to work. Some give an error like "The request is malformed or incorrect.", others go through, but doesn't include the lookup field.
The documentation for this is scarce (if any), and I have found very little information on google (apart from someone asking the same question with no answers). Anyone that got this to work yet? Is it even possible?
I have got this to work with people fields where the multiple selection option was DISABLED. So if you have a person field which allows one person only, the following works.
"{columnName}LookupId": id
Additionally, the id still needs to be the SP ID and not the graph user GUID. This ID changes from site to site.
You need to add a lookup column in the following format
"Office": "London",
"OfficeLookupId", "16"
Where Office is the name of the column

OData PUT to replace collection property

Using the OData standard is it possible to replace collection by sending a new collection?
Scenario:
The person object contains a list Address object. I would want to replace the Address collection with a new collection.
PUT Persons(1)/Addresses
[{"city": "X", "country": "US"}, {"city": "Y", "country": "US"}]
This is not possible out of the box (at least for ODATAv3), as the default routing template does not expect segments after the key portion.
But you should be able to add an ODATA Action that would do what you want to achieve. Your action definition could then look similar to this:
var action = builder.Entity<Person>()
.Action("Addresses")
.Returns<bool>();
action.Parameter<Collection<CityCountryPair>>("data");
The type CityCountryPair would be a regular DTO containing your properties you want to change. Make sure this type is also registered as an EntitySet in Odata or use a plain map/dictionary with only primitive types.
The actual call to the ODATA action would then look similar to this:
POST http://www.example.com/api/YourEndpoint/Persons(42)/Addresses
Content-Type: application/json
{
"data" :
[
{ "city" : "Berne" , "country": "CH" },
{ "city" : "Y" , "country": "CH" }
]
}
If you want to send more complex data types you can still resort to a customer JSON Deserialiser and override the default one or use a custom model binder after all.

Zapier multi step triggers and passing variables

I am getting stuck creating my own app to use within Zapier. It is for an unsupported CRM
https://www.brightpearl.com/developer/latest/
I have been able to authenticate and create a test trigger - a simple call to retrive information about one product ID where the ID is provided in the request URL manually.
Example Use Case
Using an Email sent to a GMail account, search for a customerand add the body of the email as a note to the customer.
I can search using Zapier->Searches to retrieve a result.
The Brightpearl API search returns an ID for any matched contacts.
https://www.brightpearl.com/support/documentation/resource-search
The ID can be accessed in the json response
{
response: {
results: [
[
4,
"admin#email.com",
"Primary",
"Admin"
]
]
},
reference: {}
}
The ID is required to add the note later
How do I store the ID to use in the Action later?
How do I chain the events together so that the Action is called after the ID is captured?
I have gone through the Zapier documentation and cannot find example code which does this.
The trigger that exposes the ID in the API response (in this case, the Brightpearl search result) can be mapped to a subsequent action. You don't store data in Zapier - you just pass it between actions.
Zapier's multi-step interface lets you append actions which can accept any data returned from the previous step.

How to get the id of the model I am currently viewing

So lets say I am in /post/2. Which shows me the post with the ID of 2.
Now in controller, how can I see what the current post id is that is being viewed?
My case is a bit different because I am also rendering a partial from another controller into the posts page. And if I delete one of the entries from that partial, I will also need to know the current post id that is being watched.
I'm not sure I completely understand your question, but you can access the ID of any model by simply calling the id method on it. For example if you have a model called Post and an instance of it called #post you would get the id by calling #post.id.
You can get the ID of a model by simply calling id on it. Example: #post.id
If you didn't fetch the model from the database yet, you can get the ID parameter from the URL by accessing params[:id]

Resources