gpt4 book ai didi

ruby-on-rails - RoR 3 将用户限制为每天 2 个帖子

转载 作者:行者123 更新时间:2023-12-04 03:44:17 25 4
gpt4 key购买 nike

我正在寻找限制我的用户每天发布两次以上并且每周发布不超过 5 个帖子的方法。我有一个用户和帖子模型/ Controller 。

我一直在研究这些问题,但它们并不是我想要的。

Rails 3.1 limit user created objects

How do I validate a time in Rails? Limiting a user to post once a day

错误 @ 20:44 13/03/2012 来自 KandadaBoggu 的代码

NoMethodError in PostsController#create

undefined method `beginnning_of_day' for 2012-03-13 20:36:11 +0000:Time

最佳答案

尝试这个:

class User
has_many :posts do

def today
where(:created_at => (Time.zone.now.beginning_of_day..Time.zone.now))
end

def this_week
where(:created_at => (Time.zone.now.beginning_of_week..Time.zone.now))
end
end
end


class Post
belongs_to :user

validate :user_quota, :on => :create

private
def user_quota
if user.posts.today.count >= 2
errors.add(:base, "Exceeds daily limit")
elsif user.posts.this_week.count >= 5
errors.add(:base, "Exceeds weekly limit")
end
end

end

关于ruby-on-rails - RoR 3 将用户限制为每天 2 个帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9690228/

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