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