validates_uniqueness_of ...限制范围 - 如何限制某人创建一定数量的条目

时间:2021-06-30 19:35:01

I have the following code:

我有以下代码:

class Like < ActiveRecord::Base
  belongs_to :site
  validates_uniqueness_of :ip_address, :scope => [:site_id]
end

Which limits a person from "liking" a site more than one time based on a remote ip request. Essentially when someone "likes" a site, a record is created in the Likes table and I use a hidden field to request and pass their ip address to the :ip_address column in the like table. With the above code I am limiting the user to one "like" per their ip address. I would like to limit this to a certain number for instance 10.

这限制了一个人基于远程ip请求多次“喜欢”一个站点。基本上当有人“喜欢”某个网站时,会在Likes表中创建一条记录,并使用隐藏字段来请求并将其ip地址传递给like表中的:ip_address列。使用上面的代码,我将用户的IP地址限制为一个“喜欢”。我想将此限制为一定数量,例如10。

My initial thought was do something like this:

我最初的想法是做这样的事情:

validates_uniqueness_of :ip_address, :scope => [:site_id, :limit => 10]

But that doesn't seem to work. Is there a simple syntax here that will allow me to do such a thing?

但这似乎不起作用。这里有一个简单的语法可以让我做这样的事吗?

2 个解决方案

#1


2  

You can try this:

你可以试试这个:

class Like < ActiveRecord::Base
  validates_each :ip_address do |row, attr, value|
    m.errors.add :ip_address, 'Too many likes' unless row.like_count < 10
  end

  def like_count
    Like.count(:conditions => {:ip_address => ip_address, :site_id => site_id})
  end
end

Note:

注意:

I use a hidden field to request and pass their ip address to the :ip_address 
column in the like table. 

Are you doing this to get the IP address of the client? You can get the IP address from the request object.

你这样做是为了获得客户端的IP地址吗?您可以从请求对象获取IP地址。

E.g.: In your controller/view:

例如:在您的控制器/视图中:

request.remote_ip

#2


0  

No there is no shortcut macro.

没有没有快捷方式的宏。

You will need something like:

你需要这样的东西:

validate do |record|
  if count(:conditions => ['`id` <> ? AND `ip_address` = ? AND `site_id` = ?',
                           record.id, record.ip_address, record.site_id]) > 10
    record.errors.add(:ip_address, "has been liked the maximum number of times")
  end
end

#1


2  

You can try this:

你可以试试这个:

class Like < ActiveRecord::Base
  validates_each :ip_address do |row, attr, value|
    m.errors.add :ip_address, 'Too many likes' unless row.like_count < 10
  end

  def like_count
    Like.count(:conditions => {:ip_address => ip_address, :site_id => site_id})
  end
end

Note:

注意:

I use a hidden field to request and pass their ip address to the :ip_address 
column in the like table. 

Are you doing this to get the IP address of the client? You can get the IP address from the request object.

你这样做是为了获得客户端的IP地址吗?您可以从请求对象获取IP地址。

E.g.: In your controller/view:

例如:在您的控制器/视图中:

request.remote_ip

#2


0  

No there is no shortcut macro.

没有没有快捷方式的宏。

You will need something like:

你需要这样的东西:

validate do |record|
  if count(:conditions => ['`id` <> ? AND `ip_address` = ? AND `site_id` = ?',
                           record.id, record.ip_address, record.site_id]) > 10
    record.errors.add(:ip_address, "has been liked the maximum number of times")
  end
end