English 中文(简体)
铁路:检查是否存在有多个协会
原标题:Rails: Check if associations exist in has_many

能否检查是否存在多个关联? 在这样的情况下,我想检查是否有一本有三个标签的书“a”、“b”和“c”的书。

(书本和标签通过多个链接链接到多个。 )

if Book.all.tags.find("a", "b", "c").any?
  # Yay!
  # There is at least one book with all three tags (a, b & c)
end
最佳回答

我使用此查询结构 :

Book
  .select( distinct books.* )
  .joins(:tags)
  .where( tags.slug  => [ a ,  b ,  c ])
  .group("pages.id")
  .having("count(*) = #{[ a ,  b ,  c ].size}")
问题回答

我认为这样可以把戏( has_many 上的工作) :

Book.includes(:tags).where(tags: { name: [ a ,  b ,  c ] }).any?

分解为:

包括 , 告诉铁路公司使用热负荷装载标签, 而不是一次装入标签。 这也使得铁路公司在查询中注意 tags , 并产生更聪明的查询 。

条件非常简单, 且通过数组将 比较转换为 IN (a, b, c) 条件 。

铁路对 any? 是否明智? ,因此,它不但没有装入记录(它生成了garish查询, btw),而是生成了只加载计数的记录。

我从来没有找到过一个优雅的轨迹 来解决这个问题, 所以,希望有人来 与一个更好的答案。

野蛮的方法是下降到sql。 我写这个是用记忆写的, 这样可能会有语法问题。

在书型模式中类似。表格的简称是,这样他们不会与父查询发生冲突。

def self.with_tag(tag)
    where("EXISTS (SELECT 1 from books_tags bt, tags t where bt.tag_id = t.id 
    and bt.book_id = books.id and t.name = ?)", tag)
end

然后您会调用 Book. with_tag( a). with_tag(b). with_tag(c) 或定义另一种方法。 不确定是否需要范围变量 。

def self.with_all_tags(tags) 
    scope = self.scoped
    tags.each do |t|
        scope = scope.with_tag(t)
    end
end




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签