How do I remove double square brackets in ruby on rails? - ruby-on-rails

After converting sql query from mysql object to json format I am getting array and I want only integer part from the array. Please let me know how can I achieve that.
query = "select count(priority) from table_complaint"
data = ActiveRecord::Base.connection(query).to_json
result is [[55]]
After executing above query I am getting results as [[integer]]
Now I want only integer/float part from the above array. Please help me out

You could take the first element of the outer array, which will be the inner array, and then the first element of that.
[[55]][0][0]
You might find the MiniSQL gem to be useful, but I wonder why you do not:
data = TableComplaint.count

Related

Grab the highest date inside of an array of entities.

I have the following array :
let messages = (fetchedResultsController.fetchedObjects as! [Message])
essentially, each element in this array is of the entity Message, where each Message has 5 core data attributes. Notably, the attribute I care about in this case is timestamp which is of type NSDate !!!!!!. I want to grab the message with the maximum date inside of this array using the reduce function. Please do not suggest to sort the messages first. I'am trying to avoid that. Thank you !
Not sure why you would want to use reduce, but I think this will work and fits with what you are looking for (assuming Swift 3):
let result = messages.max(by: {$0.timeStamp < $1.timeStamp})

What is simplest way to convert :pluck or :collect results to array of strings in Rails?

I have a model called Record and belongs Product model,
the price column type is hexadecimal. Rails already convert it to string in view. But I wanna get them in my console queries.
Example code for pluck:
Product.first.records.pluck(:price)
This query displays the values in array as hexadecimal. There is a method called to_sentences for pluck values but its not enough for my case.
The problem is same in collect method:
Product.first.records.collect(&:price)
What is the pluck query to display my hexadecimal data as array of strings like:
["45,46","75,42"]
Product.first.records.pluck(:price).map(&:to_s)
please try this:
Product.first.records.pluck(:price).join(',')

Check if value exist into array field mongoid rails

I have a groups field in mongodb database
"groups" : "[\"5514efcc6970640f40150100\", \"5514efcc6970640f40160100\", \"5514efcc6970640f40170100\", \"5514efcc6970640f40180100\"]
Please suggest the query to check if field is exist into above database array.
like modelname.find(:groups=> 5514efcc6970640f40150100)
Looks like you actually have a string in groups, not an array. The string looks like you called inspect on an array of BSON ObjectIds in Ruby and stored that stringified array in MongoDB.
The leading double quote here:
⬇
"groups" : "[\"5514efcc6970640f40150100\", ...
⬆
tells you that you have a string. Inside that string you have what looks like an array of strings but it is still a string.
If that's really what you have then I think you're stuck with regex searches (which will be slow), something like:
Model.where(:groups => /"5514efcc6970640f40150100"/)
That assumes that all your groups strings are consistently formed and they are really stringified arrays of hexadecimal strings.
If this is the case then I'd suggest that you immediately fix your database and code so that you use real arrays in groups.
If your groups really is an array of hex strings like:
"groups" : ["5514efcc6970640f40150100", ...
then you can query it like a scalar field and let MongoDB unwrap the array for you:
Model.where(:groups => '5514efcc6970640f40150100')

A select in rails query does not return an array

Here is the rails 3 query in the app:
Order.where('id IN (?)',ContractItem.where(contract_id: #contract.id).select('contract_item_id'))
Here is the only record in contract_item table:
As shown in the record, the #contract.id is equal to 1 and we expect ContractItem.where(contract_id: #contract.id).select('contract_item_id') returns array [2].
So Order.where('id IN (?)',ContractItem.where(contract_id: #contract.id).select('contract_item_id'))should return order#2. However what we have got is nothing. We figure an empty array must be returned instead. Is there something wrong with the code above?
You're getting back an array, but it's an array of ContractItem objects with a single column, not an array of numbers.
Try instead...
Order.where('id IN (?)',ContractItem.where(contract_id: #contract.id).pluck(:contract_item_id))

Extract data from Mysql::Object in ruby on rails

I have code to run sql query in ruby as follows
sql =
ActiveRecord::Base.connection()
sql.begin_db_transaction
report = sql.execute("select * from users;")
sql.commit_db_transaction
So after this report is an Mysql::object. Now I want to extract all fields and its corresponding data to array or hash.
thanks,
execute method should produce a result which gives you a method called all_hashes - it will return an array of hashes corresponding to the rows of query's results, which seems to be what you need. So, call
report.all_hashes

Resources