"The provided key element does not match the schema" - ruby-on-rails

I am building a rails app. I am using dynamodb for the database tables. I get the error
The provided key element does not match the schema
In my helper/controller:
session[:id] = #record.id
In my view:
<% record_id = TableName.find(session[:id]) %>
I printed the session[:id] and checked, it has the correct id of the particular record. Also checked the db. The record matches with my desired one. It works fine in rails console.
But, when I run the application, I get the above error.
Kindly help.

When you get that error from DynamoDB it is because the key you are providing doesn't match the type of the key in your table. Either your table has a key that is defined as a String and you're passing in a Number; or vice versa - the table's key is a Number and you're passing it in as a String.

Related

Postgres: Column updated but not detected.

So i initially had a foreign id tutor_id as type string. So i ran the following migrations.
change_column(:profiles, :tutor_id, 'integer USING CAST(tutor_id AS integer)')
The problem is that there was data already created which initially contained the tutor_id as type string. I did read however that by using CAST, the data should be converted into an integer.
So just to confirm i went into heroku run rails console to check the tutor_id of the profiles and tutor_id.is_a? Integer returns true.
However i am currently getting this error
ActionView::Template::Error (PG::UndefinedFunction: ERROR: operator does not exist: integer = text at character 66
Why is that so? Is the only way out to delete the data and to recreate it?
(I'm assuming the information provided above is enough to draw a conclusion, else i will add the relevant information too.)
You also have to update your code to use integers rather than strings. This error happens because your code somewhere still has the column type as string and the query sent has the value sent as '123'. PostgreSQL doesn't do automatic type conversions so it's telling you it can't do the comparison.

How do you define both a single id and multiple ids with swagger?

We have the following end point defined in swagger
products/{ids}
where ids is a list of comma separated ids. However, we get an error if there is only one id. For example products/4 gives an error whereas products/4,5 is ok and even products/4, is valid.
We are using restify with swagger-restify-mw.
How can you have both products/4 and products/4,5 to be valid end points?

How to search for a record and then delete it

How to search for the ttoken record by mobile no. and then delete that record?
User.rb:
field :ttokens, type: Hash, :default => {} # Stored as hash over numbers
Normally the value of ttokens, in the rails console, are as follows:
ttokens: {"919839398393"=>{"atoken"=>"f704e803061e594150b09ad8akabfc6105ac85ab", "confirmed"=>true}, "91812798765"=>{"atoken"=>"255cb70926978b93eik67edb52fa23a163587b4b", "confirmed"=>true}}
I need a mongodb query to search for the ttoken record by mobile number and then delete that record. The DB used is MongoDB. Any guidance or help would be highly appreciated.
You need to use MongoDB 'dot notation' for the embedded element, which means the "key" must be a string type of notation. Also apply $exists to match where the key in the has is present and the .unset() method from mongoid:
User.where('_id'=> userId, 'ttokens.919839398393'=> { '$exists' => true }).unset(
'ttokens.919839398393'
)
This is effectively the $unset operator of MongoDB, which removes "keys" from the document by the path specified.
From the sample document this would match and remove the first key, leaving only the other.

Unable to read values from object returned from ActiveRecord.find

I make the following call to the DB.
#patientRegistration = PatientRegistration.find(:all,
:conditions=>["name = '#{patientName}'"])
This searches for a patient registration based on a given name. I get a valid #patientRegistration object. When I invoke #patientRegistration.inspect it prints correctly all the values for the object in the DB.
But when I try to read a particular attribute (say id or name) by doing the following: #patientRegistration.id or #patientRegistration.name, I get invalid values. Either its blank or some junk values. I don't understand how inspect is able to retrieve all the values correctly but reading individual attributes gives invalid values.
Thanks
find(:all) returns an array of all the records that match the conditions (inspect probably shows you the result in square brackets). #patientRegistration.first.name will return you the name of the first record in the array. However, if you are only interested in the first or only record that matches the conditions, then you can use find(:first) instead:
#patientRegistration = PatientRegistration.find(:first,
:conditions => ["name = ?", patientName])
Note that I've also changed your condition to use a parameter so that it is no longer at risk from SQL injection attacks.
You can also re-write this code using an attribute-based finder:
#patientRegistration = PatientRegistration.find_by_name(patientName)
This will do a find(:first). For the equivalent of find(:all), use find_all_by instead of find_by.

rails accessing value from facebooker hash/array

This is my first time using the facebooker plugin with rails, and I'm having trouble accessing user info. The website uses FB connect to authenticate users.
I am trying to get the name of the university that the logged in user attends.
When I use the command <%= facebook_session.user.education_history[:name] %>, I get an error "Symbol as array index".
I have also tried using education_history[1], but that just returns
"# Facebooker::EducationInfo:<some sort of alphanumeric hash value>"
When I use something like <%= facebook_session.user.relationship_status %> , it returns the relationship status just fine.
Similarly,
<%= facebook_session.user.hometown_location.city %> returns the city name just fine.
I've checked out the documentation for facebooker, but I can't figure out the correct way to get the values I need.
Any idea on how to get this to work?
Thanks!
facebook_session.user.education_history returns an array of Facebooker::EducationInfo objects. The proper way to access it would be:
ed = facebook_session.user.education_history.last #Will be nil if not provided
#ed_name = ed.name unless ed.nil?
I am not sure the order they are sorted in, so you may need to call .first instead of .last

Resources