- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想做的是让一个用户每月只认可 3 个其他用户(如果可能的话,也许每个新月都会清除限制。
我试过按照这个 ror limiting users to 2 posts per day
运气不好。我在我的用户模型中创建了 this_month 方法
def this_month
where(:created_at => (Time.zone.now.beginning_of_month..Time.zone.now))
end
在我的 Endorsements 模型中:
class Endorsement < ActiveRecord::Base
belongs_to :endorser, class_name: "User"
belongs_to :endorsed, class_name: "User"
validates :endorser_id, presence: true
validates :endorsed_id, presence: true
validates :comment, presence: true, length: { maximum: 140}
validate :endorsement_count_within_limit?, :on => :create
private
def endorsement_count_within_limit?
if endorser.endorsing.this_month.count >= 3
errors.add(:base, "Exceeded endorse limit (3) this month")
end
end
end
我在尝试背书时遇到错误
NoMethodError (undefined method `this_month' for #<User::ActiveRecord_Associations_CollectionProxy:0x007fe6bd957fa8>):
app/models/endorsement.rb:11:in `endorsement_count_within_limit?'
app/models/user.rb:113:in `endorse'
app/controllers/endorsements_controller.rb:7:in `create'
我在这里缺少什么?
这是我的用户模型
class User < ActiveRecord::Base
has_many :active_endorsements, class_name: "Endorsement",
foreign_key: "endorser_id",
dependent: :destroy
has_many :passive_endorsements, class_name: "Endorsement",
foreign_key: "endorsed_id",
dependent: :destroy
.
.
.
has_many :endorsing, through: :active_endorsements, source: :endorsed
has_many :endorsers, through: :passive_endorsements, source: :endorser
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
validates :name, presence: true, length: { maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
.
.
.
# Endorses a user.
def endorse(other_user, comment)
active_endorsements.create(endorsed_id: other_user.id, comment: comment)
end
# Unendorses a user.
def unendorse(other_user)
active_endorsements.find_by(endorsed_id: other_user.id).destroy
end
# Returns true if the current user is endorsing the other user.
def endorsing?(other_user)
endorsing.include?(other_user)
end
def this_month
where(:created_at => (Time.zone.now.beginning_of_month..Time.zone.now))
end
private
.
.
.
end
背书 Controller :
class EndorsementsController < ApplicationController
before_action :logged_in_user
def create
@user = User.find(params[:endorsed_id])
comment = params[:endorsement][:comment]
current_user.endorse(@user, comment)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
def destroy
@user = Endorsement.find(params[:id]).endorsed
current_user.unendorse(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
end
我已经删除了不必要的(我认为)的东西。如果需要,其余代码(没有添加)位于 https://bitbucket.org/kramarz/pracainzynierska。
最佳答案
看起来 endorsing 是一个集合而不是一个模型,所以它没有你在两个模型上构建的 this_month
方法
尝试更改为
def endorsement_count_within_limit?
if endorser.endorsing.where(:created_at => (Time.zone.now.beginning_of_month..Time.zone.now)).count >= 3
errors.add(:base, "Exceeded endorse limit (3) this month")
end
end
看看它是否解决了这个问题。这可能可以通过将其定义为 scope 来解决。而不是方法
关于ruby-on-rails - ruby rails : Limit user to endorse(comment) 3 other users a month,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34999886/
有没有办法在 Brainfuck 中做包含句点('.')的评论? 我知道我基本上可以使用不是命令之一的每个字符,它会被忽略,但我想在文件顶部的注释中放置一个版本号,其中包含一个句点。 最佳答案 您可以
我不明白以下代码的 % comment.length 位: comment.charAt(i % comment.length()) 括号之间的部分是否会转换为整数,其值表示与评论长度相关的i? 例如
我终于找到了我的 PHP 脚本无法运行的原因。这是因为 MySQL "--comment"而不是 "-- comment"。我最近开始使用 PHP,直到那时,我一直使用“--comment”。现在,我
是否可以在项目中搜索用户在 checkin 文件后添加的评论? (手动可以通过选择一个文件,右键单击 --> 显示历史记录 --> 选择版本 --> 详细信息,在评论 Pane 中查看)。 是否可以自
我不完全确定这是否不是插件,但是在 .js 文件中开始一行注释之后,例如,当我按下回车键时,下一行也以“//”开头。这有点烦人。有没有简单的方法可以删除它? 最佳答案 在“首选项 -> 包设置 ->
我在网上搜索,没有找到解释这个评论 block 用法的结果。因此,我希望有人能向我解释这种评论风格背后的原因。 /// Text goes here. 最佳答案 它们是 XML 文档注释。 http
这有充分的理由吗?这是一个蹩脚的问题,但我只是想知道是否有原因。 最佳答案 因为 specification允许/**/但不允许//:) 不过,严重的是,CSS 像对待所有其他空格一样对待换行符,并且
我有一个评论模型,我看过使用 @comment, :comment, comment 来引用 MVC 中的对象的示例。我怎么知道哪个是哪个?有区别吗? 最佳答案 @comment指 Rails Con
我有一些使用 Drupal 实现的网站。然而,尽管 Drupal 很酷,但我从未对它的编码感到满意,主要是因为它是在 PHP 中的,而且我想使用 python。我曾与 Django 调情,但我最近才发
似乎我仍然没有完全转变为 mac 用户(来自 Windows),再次遇到键盘快捷键问题: 在 phpstorm 中,下拉菜单中显示了以下“带有行注释的注释”的快捷方式: 现在,问题是,我的键盘上没有“
我正在按照 here 所述使用 fb:comments| . 评论工作正常,但我找不到在添加新评论时收到通知的方法。有没有办法轻松找到新评论(无需每次都访问我的 3000 篇文章)? 我知道 FB 对
我正在尝试创建一个功能,用户可以在其中使用 Ajax 创建对文章的评论。但是,我不明白为什么只有存在一个评论才能通过 ajax 成功呈现评论。评论提交到数据库,没有任何回滚。 如果我重新加载页面并创建
我正在使用 mongodb native 驱动器来获取查询结果的游标。 我正在使用一个“评论”字段,它基本上是一个字符串,下面是我正在使用的代码片段 let leve1_n = 'labreports
如果我将此URL放入浏览器中:。Https://www.facebook.com/plugins/comments.php?api_key=MYAPID。它会显示注释框,但不会显示任何评论。谁能告诉我
我想显示每个项目的评论数量和最后评论的日期。 SELECT item.*, count(comments.itemid) AS commentcount, comments.created A
这个问题在这里已经有了答案: What is the second parameter of NSLocalizedString()? (5 个答案) 关闭 6 年前。 我搜索并阅读了 NSLoca
我希望我的 Wordpress 博客中的字幕能够计算我的帖子在 Facebook 上的评论数。插入Facebook的代码后 ID); ?>"> comments 我意识到当我只有 1
我在我的项目中收到以下 TsLint 消息: TsLint: comment must start with lowercase letter 这背后有什么依据吗?我同意我在 TsLint 中遇到的大
我正在使用以下代码完成教程: New Comment @comment = Comment.new, :locals => { :button_name => "Create" } %>
环境 适用于macOS的Visual Studio代码1.18.0。 主题hedinne.popping-and-locking-vscode,由 hedinne 组成。 问题 当我覆盖/*> edi
我是一名优秀的程序员,十分优秀!