[英]How to get data from 2 tables based on search keyword rails
I have 2 tables schools
and private_schools
and they are not related to each other. 我有2桌schools
和private_schools
,它们彼此不相关。 What I need to do is to search the data from both tables to populate the results. 我需要做的是从两个表中搜索数据以填充结果。
Both tables has the attribute name
from which I need to get results. 这两个表都有我需要从中获取结果的属性name
。 So when user will write some keyword the data will be searched from both tables. 因此,当用户编写一些关键字时,将从两个表中搜索数据。
Here is the query I am trying but it returns errors. 这是我正在尝试的查询,但它返回错误。
Query: 查询:
User.includes(:schools, :private_schools).where("schools.name = ? or
private_schools.name = ?", params[:keyword])
I know the User.
我认识User.
sort of thing is also useless and is also not needed but how we should cope this issue to search keywords from both tables at same time? 事情也是没有用的,也是没有必要的,但是我们应该如何解决这个问题,以便同时从两个表中搜索关键字?
Error is: 错误是:
ActiveRecord::PreparedStatementInvalid (wrong number of bind variables (1 for 2) in: schools.name = ? or private_schools.name = ?): ActiveRecord :: PreparedStatementInvalid(错误的绑定变量数(1为2):schools.name =?或private_schools.name =?):
Per comments the code of models are: 根据注释,模型代码为:
Private School: 私立学校:
class PrivateSchool < ActiveRecord::Base
belongs_to :teacher
has_many :private_school_specializations, :dependent => :destroy
has_many :private_classes
has_many :invitation_promo_codes
has_many :popular_schools, as: :resource, :dependent => :destroy
end
School: 学校:
class School < ActiveRecord::Base
belongs_to :user
has_many :students
has_many :subjects
has_many :teachers
has_many :departments
has_many :student_ids
has_many :teacher_ids
has_many :class_rooms
has_many :popular_schools, as: :resource, :dependent => :destroy
end
User: 用户:
class User < ActiveRecord::Base
has_one :school
has_one :student
has_one :teacher
accepts_nested_attributes_for :student
accepts_nested_attributes_for :teacher
end
ERROR REASON The following query is expecting two params, one to each ?
错误原因以下查询期望两个参数,每个参数?
User.includes(:schools, :private_schools).where("schools.name = ? or
private_schools.name = ?", params[:keyword])
So 所以
User.includes(:schools, :private_schools).where("schools.name = ? or
private_schools.name = ?", params[:keyword], params[:keyword])
Or just 要不就
User.includes(:schools, :private_schools).where("schools.name = :name or
private_schools.name = :name", name: params[:keyword])
EXPECTING SOMETHING ELSE? 期待其他吗?
I know the User. 我认识用户。 sort of thing is also useless and is also not needed but how we should cope this issue to search keywords from both tables at same time? 事情也是没有用的,也是没有必要的,但是我们应该如何解决这个问题,以便同时从两个表中搜索关键字?
Actually your User.
实际上是您的User.
query will return a list of users and not a list of schools or private_schools, so if you are not expecting a list of users you might be looking for something else... perhaps a list of schools? 查询将返回用户列表,而不是学校列表或private_schools列表,因此,如果您不希望看到用户列表,则可能正在寻找其他内容……也许是学校列表?
If your schools
and private_schools
have the same structure you should take a look into inheritance and use the same table for both models. 如果您的schools
和private_schools
具有相同的结构,则应该研究继承 ,并对两个模型使用相同的表。 For now you could do something like: 现在,您可以执行以下操作:
def my_result
schools = School.where(name: params[:name]).to_a
private_schools = PrivateSchool.where(name: params[:name]).to_a
schools + private_schools # combine both arrays
end
I have couple of things for you: 我有几件事适合您:
The includes
is used to specify relationships to be included in the result set. includes
用于指定要包含在结果集中的关系。 So you don't have any relationship between schools
and private_schools
. 因此,您与schools
和private_schools
之间没有任何关系。 So the straight forward you can't query it like this because you can't join
two tables without having any relation. 因此,您不能像这样直接查询它,因为您不能在没有任何关系的情况下join
两个表。 You can do a separate query for each table school
and private_school
. 您可以为每个table school
和private_school
单独查询。
schools = School.where(name: params[:name]) schools << PrivateSchool.where(name: params[:name]) schools.flatten!
You can put both into an array like this and then loop on the view. 您可以将它们都放入这样的数组中,然后在视图上循环。 To just take out the names you can add: 要仅删除名称,您可以添加:
pluck(:name)
The includes is different thing. 包含是不同的东西。 Like for example: 例如:
User.includes(:posts).where('posts.name = ?', 'example')
when the Post
belong to the User
so what will happen is the include
will load the posts
and when you try to fetch the posts
with example
name it would not fire any additional query as it has that data already loaded. 当Post
属于User
, include
将加载到posts
并且当您尝试使用example
名称获取posts
,由于已加载了该数据,因此不会触发任何其他查询。 So this only works when you have a relation between the models. 因此,这仅在模型之间具有关系时才有效。
school
and private_school
can be one single model if the attributes of both are same. 如果school
和private_school
的属性相同,则它们可以是一个模型。 You can just add an attribute of is_private
which would be boolean and its value will be true
in case of private schools. 您可以仅添加一个is_private
属性,该属性为boolean,如果是私立学校,则其值为true
。 (This will depend on your attributes) (这取决于您的属性) If you are going to do "complex" queries such as this frequently in your application, I highly recommend you check the "ransack" gem. 如果您打算在应用程序中经常执行诸如此类的“复杂”查询,我强烈建议您检查“ ransack” gem。 It makes queries like this a breeze, and your codebase clean. 它使这样的查询变得轻而易举,并且您的代码库也很干净。
MyModel.search(:private_school_name_or_school_name_cont => params[:q).result
ref: https://github.com/activerecord-hackery/ransack 参考: https : //github.com/activerecord-hackery/ransack
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.