Ruby on rails Rails 5具有多个关键字的自定义查询
我有一个Ruby on rails Rails 5具有多个关键字的自定义查询,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,我有一个产品和标签模型,该模型通过联接表产品标签具有多个关联。我有许多产品与石灰石标签和许多与模塑标签,但我想只显示那些产品都有。我知道我缺少了一些Rails/Ruby的基本功能,但我还是太傻了,看不到它 我正在尝试的下面的代码给了我一个错误PG::UndefinedTable:error:missing FROM子句条目中的表“tag” 产品\u controller.rb ... def index if params[:query] @products = Pr
产品
和标签
模型,该模型通过联接表产品
标签具有多个关联。我有许多产品与石灰石标签和许多与模塑标签,但我想只显示那些产品都有。我知道我缺少了一些Rails/Ruby的基本功能,但我还是太傻了,看不到它
我正在尝试的下面的代码给了我一个错误PG::UndefinedTable:error:missing FROM子句条目中的表“tag”
产品\u controller.rb
...
def index
if params[:query]
@products = Product.search_for(params[:query]).includes(:tags)
@mouldings = @products.where('tag.name LIKE ? AND tag.name LIKE ?', 'Mouldings', 'Limestones')
else
...
end
end
...
尝试使用以下
标记
代替标记
@mouldings = @products.where('tags.name ILIKE ? AND tags.name ILIKE ?', 'Mouldings', 'Limestones')
因为您的表名是tags
而不是tag
,并且它清楚地指示
PG::UndefinedTable:错误:表“tag”的子句条目中缺少
更新
@products = Product.search_for(params[:query]).where('true')
@mouldings = @products.includes(:tags).where('tags.name ILIKE ? AND tags.name ILIKE ?', '%Mouldings%', '%Limestones%')
更新2
@mouldings = @products.includes(:tags).where('tags.name ILIKE ?', '%Mouldings%')
@limestones = @mouldings.where('tags.name ILIKE ?', '%Limestones%')
尝试使用以下
标记
代替标记
@mouldings = @products.where('tags.name ILIKE ? AND tags.name ILIKE ?', 'Mouldings', 'Limestones')
因为您的表名是tags
而不是tag
,并且它清楚地指示
PG::UndefinedTable:错误:表“tag”的子句条目中缺少
更新
@products = Product.search_for(params[:query]).where('true')
@mouldings = @products.includes(:tags).where('tags.name ILIKE ? AND tags.name ILIKE ?', '%Mouldings%', '%Limestones%')
更新2
@mouldings = @products.includes(:tags).where('tags.name ILIKE ?', '%Mouldings%')
@limestones = @mouldings.where('tags.name ILIKE ?', '%Limestones%')
现在我得到
PG::UndefinedTable:ERROR:表“tags”第1行的子句条目中缺少:…search\u 0a3e27b8ca818264d75c8d.PG\u search\u id在哪里(tags.name…
@StudioRooster查看此更新部分,更新了答案我现在得到一个没有产品的空白页,因此我决定尝试删除一个绑定变量,只使用'tags.name ILIKE?'、'%Mouldings%'
,它成功了,我将所有产品标记为模塑件
——我尝试了石灰石
太多了,所有的产品都被标记为石灰岩
。添加多个绑定变量似乎与某些事情相冲突。是的,这是因为和意味着这个产品和模具
和石灰岩
现在我得到了PG::UnfinedTable:错误:表“tags”的子句条目中缺少第1行:…搜索\u 0a3e27b8ca818264d75c8d.pg \u搜索\u id在哪里(tags.name…
@StudioRooster查看此更新部分,更新了答案我现在得到一个没有产品的空白页,因此我决定尝试删除一个绑定变量,只使用'tags.name ILIKE?'、'%Mouldings%'
,它成功了,我将所有产品标记为模塑件
——我尝试了石灰石
太多了,所有的产品都贴上了标签。石灰石
。似乎添加多个绑定变量与某些内容冲突。是的,这是因为和意味着该产品和模塑件
和石灰石