gpt4 book ai didi

ruby-on-rails - ruby rails : Limit user to endorse(comment) 3 other users a month

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:04 24 4
gpt4 key购买 nike

我想做的是让一个用户每月只认可 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com