I have 3 tables, and in params I'm getting table names and column names.
Based on params I have to query on database table.
How to get the one to one associations records based on params (multiple tables and columns in params)?
Related
I would like to retrieve all of one table and all joined records in another.
I would like to have all columns from both tables
This is extremely simple in SQL
e.g.
SELECT *
FROM students
JOIN teachers
ON students.id = teachers.student_id
How can I do the same in rails?
I've tried variations on
Student.includes(:teacher)
and
Student.joins(:teacher).includes(:teacher)
The join is working, but I cannot access columns from Teacher table
Note that the end goal is simply to be able to create an instance variable in the controller so that I can access both student and teacher data in the view
Student.includes(:teacher) will return ActiveRecord::CollectionProxy which means if take particular object in this collection, it will be Student class object.
Unlike sql query fired and returning data from 2 tables, it does not work same in rails, you get data only from students column which will relate associated record in teachers table because it represent Student model.
You can access further teachers data like,
students = Student.includes(:teacher)
students.last.teacher.name
In above no new query will get fired in database when you call teacher association on object
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
How can I insert or save multiple ids as comma separated e.g (2,5,8,10) values in single column in database for many to many relationship? I am using active admin for resource management.
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
My DB has a generic structure which was created to support alot of different type of things.
Therefore there is a Product table with 5 columns but then there is a AttributeGroup and Attribute table.
The AttributeGroup specifies extra types of properties of the Product, So there are say 30 rows in AttributeGroup all with an ProductID relating back to the Product Table. Then for every instance of the Product there is an entry in the Attribute Table which holds the value for that instance of the Product. Thus Attribute table has a AttributeGroupID and a ProductID.
There is a stored procedure which returns the most usual properties of the the AttributeGroup Table plus the initial 5 columns of the Product table. The sproc takes ProductID and an extra string input which we put a comma-deliminated string of the names of the extra fields in AttributeGroup Table. It then returns a set of all instances of the Product and joins AttributeGroup and Attributes, uses the SQL Pivot command and returns all the data in columns.
However sometimes the data has 8 columns sometimes 15. The EntityFramework calls the sproc but only returns 8 primary fields. It uses a complex type. Is there anyway to use a List or Dictionary object to load any extra column over the initial 8 so this can be binded to some view.