Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 按标记名或资源名搜索资源_Mysql_Ruby_Search_Ruby On Rails 4_Tags - Fatal编程技术网

Mysql 按标记名或资源名搜索资源

Mysql 按标记名或资源名搜索资源,mysql,ruby,search,ruby-on-rails-4,tags,Mysql,Ruby,Search,Ruby On Rails 4,Tags,我已经用这个创建了一个搜索。一切都很顺利。我可以按名称搜索所有资源 如何按名称或具有该名称的标记搜索资源? if I search for the word "Tutoring" in my text_field. I should get all resources that contain the word "Tutoring" in the name, And all the resources that have the Tag "Tutoring". Ex: if I searc

我已经用这个创建了一个搜索。一切都很顺利。我可以按名称搜索所有资源

如何按名称或具有该名称的标记搜索资源?

if I search for the word "Tutoring" in my text_field.

I should get all resources that contain the word "Tutoring" in the name, 
And all the resources that have the Tag "Tutoring".
Ex:

if I search for the word "Tutoring" in my text_field.

I should get all resources that contain the word "Tutoring" in the name, 
And all the resources that have the Tag "Tutoring".
我的当前代码一直出现此错误

Mysql2::Error: Column 'name' in where clause is ambiguous: 
SELECT COUNT(DISTINCT `resources`.`id`) FROM `resources` 
LEFT OUTER JOIN `resource_mappings` 
ON `resource_mappings`.`resource_id` = `resources`.`id` LEFT OUTER JOIN
`tags` ON `tags`.`id` = `resource_mappings`.`tag_id` WHERE (name like '%Tutoring%') 
AND (tags.name like '%Tutoring%')
模型

类资源
看起来属性
name
在您尝试访问的多个表中使用。您需要在
name
之前添加
table\u name.
,使其看起来像
WHERE(table\u name.name like…
)。只需将
table\u name
替换为要将名称与之进行比较的表和字段:-)

这就是我最后使用的代码

if name.present?    
  resources = resources.includes("tags").where("tags.name like :name OR resources.name like :name", {:name => "%#{name}%" })
end