简体   繁体   English

在Ruby on Rails中,如果注释包含很多标签,如何检查注释是否包含任何标签?

[英]In Ruby on Rails, if a note has many tags, how do I check if a note has any tags?

I tried note.tags.nil?, but it didn't work. 我尝试了note.tags.nil ?,但是没有用。 How do I do this? 我该怎么做呢? Thanks for reading. 谢谢阅读。

如果您想知道@note是否具有任何关联的tags ,可以使用

@note.tags.empty?

You can call @note.tags.any? 您可以致电@note.tags.any? to test if there are any tags. 测试是否有任何标签。 Note that this will hit the database to do a count. 请注意,这将对数据库进行计数。 You might want to left join when pulling out @note , to save this query. 您可能希望在拉出@note@note ,以保存此查询。 Eg.: 例如。:

Note.first(:select => 'notes.*, case when tags.id is not null then 1 else 0 end as has_any_tags', :joins => "LEFT JOIN tags ON tags.note_id = notes.id")

Your Note model will now have a field has_any_tags that is either 0 (false) or 1 (true), if there are any related tags. 现在,如果有任何相关标签,则您的Note模型将具有一个has_any_tags字段,该字段为0 (假)或1 (真)。

You can move the query options above into a default scope and wrap the field up in an accessor: 您可以将上面的查询选项移至默认范围,并将字段包装在访问器中:

class Note
  has_many :tags
  default_scope :select => 'notes.*, case when tags.id is not null then 1 else 0 end as has_any_tags', :joins => "LEFT JOIN tags ON tags.note_id = notes.id"
  def has_any_tags?
    has_any_tags == "1"
  end
end

Now, it all happens transparently: 现在,这一切都是透明的:

>> Note.first.has_any_tags?
=> true

In Addition, you could think about having a counter cache column for Tags. 此外,您可以考虑为标签添加一个计数器缓存列。 With that :tags_count you wouldn't need another SQL query. 有了:tags_count您就不需要另一个SQL查询了。 You could check then with @note.tags_count == 0 , @note.tags_count.zero? 您可以使用@note.tags_count == 0来检查, @note.tags_count.zero? or whatever you like. 或任何你喜欢的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Ruby on Rails中使用Evernote gem创建注释? - How do I create a note using the Evernote gem in Ruby on Rails? 带有不同标签的Rails`has_many through` - Rails `has_many through` with distinct tags 如何查询具有3个标签的活动? - How do I query for an Activity that has 3 tags? 在Rails 3.1中,如何检查模型的has_many关联的任何实例是否已更改? - In Rails 3.1 how can I check if any instances of a model's has_many association have changed? 如何在Ruby on Rails中通过关联订购has_many? - How do I order a has_many through association in Ruby on Rails? Ruby on Rails:有并且属于许多关系:如何销毁关系? - Ruby on Rails: Has and belongs to many relationship: how do I destroy a relationship? 在Ruby on Rails中,如何查询has_many:through关系中没有关联元素的项目 - In Ruby on Rails, how do I query for items that have no associated elements in a has_many :through relationship 我如何建立一个has_many关联,其中一个模型在Ruby on Rails中具有两组jsonb id - How do I set up a has_many association where one model has two sets of jsonb ids in Ruby on Rails 在将博客文章存储到数据库之前,如何通过has_many通过关系找到任何现有标签? - How to find any existing tags through a has_many - through relationship, before storing a blog post to the database? 如何通过在rails中通过关联对has_many进行关联来实现has_many? - How do I do a has_many through association on a has_many through association in rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM