Querying in MongoDB using Rails - ruby-on-rails

I have a Product class which contains about 2 million of data. Each row in Product table has a field called property which contains hash values. I have search box and the value entered in search box should be searched within the hash values of the Products table. How do I do that? I am using MongoDB and Rails. This is what I tried so far.
#product3= Product.select{|hash| hash.property.has_value? /.*#{params[:search]}*./i}
It returns undefined method 'select' for Product class. So I tried like below.
#product3= Product.all.to_a.select{|hash| hash.property.has_value? /.*#{params[:search]}*./i}
But it is looping to all the data(2 million) which takes bunch of time.
EDIT: Image for sample data from Products table.
I have shown 2 records from products table using the query Product.limit(2).all.to_a.

Try This one For Like Query
/.*#{params[:search]}.*/i

Related

Is it possible to get column names from an relation even if result rows are zero?

In Rails, one uses ActiveRecord for querying the database. ActiveRecord's query results in an ActiveRecord::Relation object. Since we can execute ActiveRecord::Relation#select and specify arbitrary SQL select clause, sometimes the records returned by the database contains columns which does not exist in the database.
If this relation contains more than one row, then one can get the column names of the relation by using the_relation.first.attributes. When no records were returned by the query, however, this method is not possible.
Question
Is there any way to get the Query's resulting column names of an ActiveRecord::Relation even if no rows were returned?
The motivation
For example, when you're building an Daru::DataFrame instance or some other Relational Data, you'd want to obtain the attribute names even if there is no records in the result.
Yes you can get the column names
If the result is ActiveRecord::Relation then you can use something like this
the_relation.column_names

Get rows from a table based on ID field and display as JSON/XML

I have a grails application that has a users table and a results table, i need to be able to get all results from the results table that match the current CustomerID that is logged in and need to have the option to display them as JSON or XML.
When i try and use:
Results.findByCustomerID
I don't get the option to pass a customer ID in like i do if i use:
Results.findByNumber()
My results table consists of a customer ID, Number, Result, Convert Service, Date, Time.
I'm doing this in a Results controller at the moment.
Any help?
I fixed this. My Domain class property began with a capital letter!

How to get right values from array thats values are in other table

I have the list of records saved as array in database like below:
---
- '9'
- '10'
- '11'
These are saved in option_ids column in table.
I have another table in which they all are present like below.
What I need to do is to print the values text like speak well if its id is present in options_ids column. So, what will happen is, if options_ids contains 9,10,12 etc so we will print data from other rows table like speak well, read well, listen well.
Assuming your "other table" is class OtherTable and assuming your fourth column is called text then you'd want to do
options_ids.map{|option| OtherTable.find(option).text}.join(', ')
When using rails you should take advantage of Active Record Associations.
I guess that a user(?) can choose different options from that second table.
The association would be a has_and_belongs_to_many-relation.
A good read is this section in the rails guide: http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
Basically you set up a join table between users and options and tell the two models that they have a has_and_belongs_to_many-relation with each other.
Then when you fetch a user you can simple call user.options and it shows the options that are related to that user object.

ListItemRenderer 'data' parameter

I am using Listitemrenderer(Zk UI and Grails) to render a list of books from database table called "book". I have different types of books in the table. the book types are determined by the bookType attribute. The issue I am having is that when I do:
Book bookInstance = (Book) data
it shows null point exception when there are different types of books in the table. But in the case when there are books of a single type, it works fine.
Is there a way to filter the parameter 'data' of Listitemrenderer so that I could get books of a certain type I want? Again, there is an attribute bookType in database table that distinguishes different types of book.
Thank you!!!
you can use Query String with 'where' clause specifying 'book_type'.
this way use can fetch the desired result.

Form for one model with many objects

UPD.
I need to display multiple records of a single model in a form.Then after updating some values I need to save all the records data at once..that is my form view should work like a spread sheet.
The requirement is, from one table I need to get data and populate all rows in a form. After that I have to edit some columns then and I should save all records of this model to database at a time.
Here is Railscast which presents how to edit multiple records in one form.

Resources