[英]Utilizing rails helper and controller methods
Suppose I have a Rails app that deals with Posts and Comment objects. 假设我有一个处理Posts和Comment对象的Rails应用程序。 A Post
has_many
Comments and each Comment belongs_to
a Post. 一个帖子
has_many
评论和每个评论belongs_to
一个帖子。
Each Comment has a word_count
property. 每个注释都有一个
word_count
属性。 The Post object has an average_comment_word_count
property which is an average of each of the Comment's word_count
. Post对象具有
average_comment_word_count
属性,该属性是每个Comment的word_count
的word_count
。
First question is if the Post object gets modified asynchronously (comments get added which affects the average word count), at what point should I recalculate the property? 第一个问题是Post对象是否被异步修改(添加了影响平均字数的注释),我应该在什么时候重新计算该属性? When the object is returned?
对象何时返回? Or each time a new comment is added?
还是每次添加新评论? Does it go into the comment or post helper methods?
它会进入注释或发布辅助方法吗? Which controller function should call this method?
哪个控制器函数应调用此方法?
Also when I include the following Post helper method, I get a NULL value returned as JSON. 同样,当我包含以下Post helper方法时,我得到的NULL值作为JSON返回。
def average_word_count
@average_word_count = 0
# current_user returns the current user object
# user has_many posts and each post belongs_to a user
current_user.posts.find(params[:id]).comments.each do |comment|
@average_word_count += comment.word_count / current_user.posts.find(params[:id]).comments.count
end
@average_word_count
end
class Comment < ActiveRecord::Base
belongs_to :post
after_save :update_post_word_count
def update_post_word_count
average_wc = post.comments.average(:word_count)
post.update_attributes average_comment_word_count: average_wc
end
end
Or, derive it only when you need it: 或者,仅在需要时派生它:
class Post < ActiveRecord::Base
has_many :comments
def average_comment_word_count
comments.average :word_count
end
end
Or, if it's just used once somewhere with low traffic, brazenly flout the Law of Demeter and just calculate it as needed from a post object: 或者,如果仅在交通流量低的地方使用过一次,则肆无忌fl地嘲笑Demeter律,并根据需要从post对象中进行计算:
Average Comment Word Count: <%= @post.comments.average :word_count %>
Update: As @coreward notes, the first part of this answer isn't useful for asynchronous updates, but the rest of the answer may still be helpful. 更新:正如@coreward所指出的那样,此答案的第一部分对于异步更新没有用,但其余答案可能仍然有用。
You would be a lot better off just building a custom counter cache based on what's already in ActiveModel that keeps track of the total number of words , then just count comments to do math manually. 仅根据ActiveModel中已有的内容(跟踪单词总数)构建自定义计数器缓存,然后仅对注释进行计数以手动进行数学运算,会好得多。
# you need a comments_count column and a words_count column in this table
class Post < ActiveRecord::Base
has_many :comments
def avg_words_per_comment
words_count / comments_count
end
end
class Comment < ActiveRecord::Base
belongs_to :post, :counter_cache => true
after_save { update_counters(post.id, :words => word_count }
before_destroy { update_counters(post.id, :words => -word_count }
end
# And in your view:
<p>
The average comment for this post has <%= @post.avg_words_per_comment %> words.
</p>
Then you don't need to worry about asynchonicity and the calculation on view is minimal. 然后,您无需担心异步性,并且视图上的计算量很小。
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/counter_cache.rb#L65 https://github.com/rails/rails/blob/master/activerecord/lib/active_record/counter_cache.rb#L65
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.