我尝试了笔记。 我如何这样做? 感谢阅读。
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 ...
我尝试了笔记。 我如何这样做? 感谢阅读。
如果你想知道@note
是否有任何关联tags
,你可以使用
@note.tags.empty?
在增补中,你可以考虑为塔格斯设立一个反切栏。 随附<代码>:标签_count,你将不需要再问。 然后,请上@note.tags_count= 0
,@note.tags_count.zero?
或你喜欢什么。
如果存在任何标签,请打电话@note.tags.any?
。 请注意,这将打到数据库做计数。 您在退出@note
时可能要加入,以挽救这一疑虑。 例如:
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")
页: 1 如果存在任何相关的标签,即为<代码>0(false)或1
(true)。
您可将上述询问选项移至缺省范围,并将实地情况总结为出人:
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
现在,这一切都是透明地发生的:
>> Note.first.has_any_tags?
=> true
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 ...
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 ...
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 : ...
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?
I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...
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
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 ...
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: ...