(Using Rails 6.1 - Ruby 3.0.1)
I have a params.permit method in Rails in the next way:
def my_nested_params
params.require(:lead).permit(
:lead_id, :quote_type, :request_a_call,
auto_data: %i[id first_name last_name email phone_type phone quote_type rent_or_own address1 address2 address_city address_state
address_zip address_county health_cover_accident_inj same_garaged garage_address1 garage_address2 garage_address_city garage_address_county
garage_address_state garage_address_zip previous_residence previous_address1 previous_address2 previous_address_city previous_address_county
previous_address_state previous_address_zip current_carrier current_carrier_length why_no_insurance auto_coverage_level length_at_dwelling prior_liability
continuous_coverage effective_date]
)
end
As you can see I a using something called percent literals. Now the issues comes when I want to add nested percent literals
Like this:
def my_nested_params
params.require(:lead).permit(
:lead_id, :quote_type, :request_a_call,
auto_data: %i[id first_name last_name email phone_type phone quote_type rent_or_own address1 address2 address_city address_state
address_zip address_county health_cover_accident_inj same_garaged garage_address1 garage_address2 garage_address_city garage_address_county
garage_address_state garage_address_zip previous_residence previous_address1 previous_address2 previous_address_city previous_address_county
previous_address_state previous_address_zip current_carrier current_carrier_length why_no_insurance auto_coverage_level length_at_dwelling prior_liability
continuous_coverage effective_date
Nested --> drivers: %i[id _destory first_name last_name dob gender marital_status education industry occupation current_license_state age_first_licensed state_first_licensed healthcare_plan]
]
)
end
The Data I am sending to my server is currently structured like this:
"input": {
"quoteType": "auto",
"autoData": {
"firstName": "John",
"lastName": "Doe",
"phone": "6149432397",
"phoneType": "mobile",
"email": "sample#email.com",
"rentOrOwn": "Home (owned)",
"address1": "912 D St",
"addressCity": "Lincoln",
"addressZip": "68502",
"addressState": "NE",
"previousResidence": "no",
"sameGaraged": "yes",
"currentCarrier": "Alfa Alliance",
"priorLiability": "300CSL",
"continuousCoverage": "14 Years",
"lengthAtDwelling": "3 Years",
"effectiveDate": "2022-06-01",
"drivers": [
{
"firstName": "John",
"lastName": "Dow",
"dobMonth": "03",
"dobDay": "13",
"dobYear": "1993",
"gender": "Male",
"maritalStatus": "Single",
"education": "Bachelors",
"industry": "Lgl/Law Enfcmt/Sec",
"occupation": "Examiner",
"currentLicenseState": "NE",
"ageFirstLicensed": 16,
"stateFirstLicensed": "NE"
}
],
"currentCarrierLength": "13 Years"
}
}
Adding the nested percent literal, Rubocop says: "Within percent literals, nested percent literals do not function and may be unwanted in the result. (warning:Lint/NestedPercentLiteral)"
And of course I keep having: Unpermitted parameter: :drivers.
What am I missing?
Related
I am trying to retrieve data from 2 buckets, no error but nothing shows up (I do have documents I need in these buckets).
1st bucket: a_bucket
here is the document I am interested in (I do have 3 different docs)
author_ID document:
{
"author_ID": 1,
"profil_creation_date": "2017/01/01/01:23:05/+5",
"prefix": "Mr.",
"first_name": "Dylan",
"middle_name_s": "Alfred",
"last_name": "Kerr",
"date_of_birth": "1974/01/02",
"sex": "M",
"marital_status": "Single",
"mobile_phone": "(860) 231-3336",
"address": [
{
"address_1": {
"address_ID": 1,
"home_address": "338 Counts Lane",
"city": "West Hartford",
"province/state": "CT",
"postal_code": "06105"
}
},
{
"address_2": {
"address_ID": 2,
"work_address": "977 Copperhead Rd",
"city": "Newington",
"province/state": "CT",
"postal_code": "06111"
}
}
]
}
2nd bucket: b_bucket
here are the 2 docs I am interested in:
p_output_ID document:
{
"p_output_ID": 1,
"author_ID": 2,
"overall_score": 4.41,
"status": {
"r_status_first": "TRUE",
"r_status_second": "FALSE",
"r_status_third": "YES",
"y_status_second": "TRUE",
"y_status_third": "FALSE",
"g_status_third": "TRUE"
}
}
timing_ID document:
{
"timing_ID": 1,
"p_output_ID": 1,
"author_ID": 1,
"date_and_time": "2017-06-06/23:45:25.25/+5",
"time_in_seconds": 12525,
"incremental_time_in_seconds": "time_in_seconds",
"current_state_and_duration": {
"state": "RED",
"duration_in_seconds": 33333
}
}
my goal is to grab these informations in one query ():
prefix, first_name, middle_name_s, last_name (from author_ID document in a_bucket)
overall_score (from p_output_ID document in b_bucket)
date_and_time, state (from timing_ID document in b_bucket)
Here is my query:
select p2.current_state_and_duration.state, p1.overall_score, p2.date_and_time
from proc_data_bucket p1 USE KEYS "p_output_ID"
JOIN proc_data_bucket p2 ON KEYS "author_ID";
The syntax is OK, but I am getting no data
Please help me with that...
CREATE INDEX ix1 ON b_bucket(timing_ID);
SELECT p1.prefix, p1.first_name, p1.middle_name_s, p1.last_name,
p2.date_and_time,p2.state,
p3.overall_score
FROM b_bucket p2
JOIN a_bucket p1 ON KEYS ("author_" || TO_STRING(p2.author_ID))
JOIN b_bucket p3 ON KEYS ("p_output_" || TO_STRING(p2.p_output_ID))
WHERE p2.timing_ID BETWEEN 10 AND 50;
I currently have a nested JSON object which resembles
{
"People": [
{
"Name": "James",
"Age": "18",
"Gender": "Male",
"Sports": []
},
{
"Name": "Sarah",
"Age": "19",
"Gender": "Female",
"Sports": [
"Soccer",
"Basketball",
"Football"
]
}
]
}
Being new to Ruby, I aim to filter throught the entire json and return only the json object/objects in which the "Sports" array has content. So in the above scenario I expect to obtain the object below as a final outcome:
{
"Name": "Sarah",
"Age": "19",
"Gender": "Female",
"Sports": [
"Soccer",
"Basketball",
"Football"
]
}
Will I have to initiate a new method to perform such an act? Or would using regular ruby calls work in this case?
Although #philipyoo answer is right, it miss an explanation on how to "filter" the parsed JSON. If you are new to ruby, take a look at Array#keep_if : http://ruby-doc.org/core-2.2.0/Array.html#method-i-keep_if
require 'json'
people = JSON.parse("{long JSON data ... }")
people_with_sports = people.fetch('People', []).keep_if do |person|
!person.fetch('Sports', []).empty?
end
If you're getting a JSON object from a request, you want to parse it and then you can traverse the hash and arrays to find the information you need. See http://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html
In your case, something like this:
require 'json'
parsed_json = JSON.parse('{"People": [ ... ]}')
parsed_json["People"].each do |person|
puts person if person["name"] == "Sarah"
end
I have two models. For example Person and Address.
Because I want to add or update addresses to the person within one request, the person model looks like:
has_many :addresses
accepts_nested_attributes_for :addresses
In the address controller is only one validation
validates :city, presence: true
When I now update the user via json api it works like a charm:
{
"user": {
"addresses_attributes": [
{"street": "bla", "zip": "12345", "city": "blubb"},
{"street": "blu", "zip": "98765", "city": "blebb"}
]
}
}
Now I delete the city of the second record:
{
"user": {
"addresses_attributes": [
{"street": "bla", "zip": "12345", "city": "blubb"},
{"street": "blu", "zip": "98765"}
]
}
}
and in the Users controller I can render a json response, something like:
render json: #user.errors
which gives me the correct error.
I am missing, that I don't know which of the addresses threw the error (In this example the second).
Any Ideas?
You can return the entire user object with its nested attributes and errors. I.e.
render json: #user.as_json(
include: [{addresses: {methods: [:errors]}],
methods: [:errors]
)
The result should look like this:
{
"user": {
"errors": {...},
"addresses_attributes": [
{"street": "bla", "zip": "12345", "city": "blubb", "errors": {...}},
{"street": "blu", "zip": "98765", "errors": {...}}
]
}
}
The #user that you tried to create will still contain the addresses that it tried to create with the nested attributes.
I don't know exactly how you want to render the fact that an address failed the validation but you can identify the one(s) that did fail by iterating over the #user.addresses.
For example, this will return all the invalid addresses:
#user.addresses.select { |address| !address.valid? }
You can still render these objects, or the json representation of them, even though they haven't been saved to the database.
I'm trying to implement custom attributes for Intercom.io into my iOS app. I'm in XCode 7, Swift 2 and iOS 9.
Here's my code:
class func updateUser(user: User) {
Intercom.updateUserWithAttributes([
"name": user.name,
"email": user.email
])
let userAttributes = ([
"role": "customer",
"phone": user.phone,
"First Name": user.firstName,
"Last Name": user.lastName,
"Referral Code": user.referralCode,
"Avatar Pic": user.avatarURL,
"Profile Pic": user.profilePicURL
])
Intercom.updateUserWithAttributes(["custom_attributes": userAttributes])
}
I am successfully submitting "name" & "email" but my "custom_attributes" aren't working. I think my syntax is correct according to the Intercom's documentation:
https://docs.intercom.io/Install-on-your-mobile-product/configuring-intercom-for-ios
But I'm a Swift newbie and have no experience with Obj-C.
Also important to note that my events are reporting properly through.
Intercom.logEventWithName(eventName)
Is there anything wrong with my syntax?? Or anything else? Please help!
Turns out there were two issues:
1) Intercom documentation wrong and "custom_attributes" tag not needed for custom attributes
2) my URL formats were NSURL and not Strings and therefore the whole object was getting rejected
Fixed now! Thanks to Dale from Intercom for the support
the code is simply:
let userAttributes = [
"name": user.name,
"email": user.email,
"role": "customer",
"phone": user.phone,
"first_name": user.firstName,
"last_name": user.lastName,
"referral_code": user.referralCode,
"avatar_pic": user.avatarURLString,
"profile_pic": user.profilePicURLString
]
Intercom.updateUserWithAttributes(userAttributes)
I have two json objects as below:
obj1= [ { "id": 4, "userId": "abc", "firstName": "abc", "lastName": "abc", "email": "abc#abc.it", "prefers" : [{"breakfast" : "bread" , "lunch" : "non-veg"}] } ]
obj2= [ { "id": 5, "userId": "def", "firstName": "def", "lastName": "def", "email": "def#def.it", "prefers" : [{"breakfast" : "egg" , "lunch" : "veg"}] } ]
Given these to objects i have to validate object2 has the same keys as in object1
diff(obj1,obj2) should give me missing keys
use keys to return an array of keys of the hash and then subtract them
obj1[0].keys - obj2[0].keys
# => array of missing keys
This Ruby JSON comparator will show you how to do it. It is designed to compare the two objects and return true if they're same, but from that you can devise a more complicated return value based on your needs.
If you only want true/false validation that the keys of both objects match, you can do:
object1.keys && object2.keys == object1.keys
That will give you a validation of matching or not.