简体   繁体   English

Ruby on Rails,验证与字符串连接的属性的唯一性

[英]Ruby on Rails, validate uniqueness of an attribute concatenated with a string

I have an attribute, :group_name , that has to be validated for uniqueness on the client side. 我有一个属性:group_name ,必须在客户端验证其唯一性。 BUT first, I need to attach a string to the attribute and only then validate it. 但是首先,我需要将字符串附加到属性,然后才对其进行验证。 Please, do not ask me why, I need it=) 拜托,不要问我为什么,我需要=)

I have a form: 我有一个表格:

 <%= form_for Group.new, url: what_to_do_groups_path ,method: :get ,:validate => true do |f| %>
 <div class="field_label">
<%= f.label :group_name%>
</div>
<div class="field">
<%=f.text_field :group_name %>
</div>
<%= submit_tag "Submit",  :commit =>"submit" %>

<br class="clear" />
<br />
<%end%>

controller: 控制器:

attr_accessible  :group_name
validates :group_name,      :uniqueness => { :case_sensitive => false}

So, the user types in "RedBull". 因此,用户键入“ RedBull”。 The :group_name has a value of "RedBull". :group_name的值为“ RedBull”。 I want to attach a string "_Marc" and only validate (client side) in the database, if there is another group called "RedBull_Marc" How can I do it? 我想附加一个字符串“ _Marc”,并且仅在数据库中进行验证(客户端),如果还有另一个名为“ RedBull_Marc”的组该怎么办?

May I do something like this? 我可以做这样的事情吗?

validates :group_name+"_Marc",      :uniqueness => { :case_sensitive => false}

And if I have multiple strings to attach in order to check if such groups are already in the database. 如果要附加多个字符串以检查数据库中是否已存在此类组。 Like I want to check if "RedBull_Marc" or "RedBull_Kate" are in the database So I can do it with regex, like this : /_(Marc|Katja)/ but where should I build it? 就像我想检查“ RedBull_Marc”或“ RedBull_Kate”是否在数据库中一样,因此我可以使用正则表达式来做到这一点,例如:/ _(Marc | Katja)/但应该在哪里构建它?

Thanks in advance. 提前致谢。

edit: 编辑:
Client side validation should be present. 客户端验证应该存在。

You could use a before_validation hook to modify the attribute before it gets validated. 您可以使用before_validation挂钩在属性验证之前对其进行修改。 You could also write your own custom validation that did the concatenation, but it's probably best to use before_validation as that's what it is meant for. 您也可以编写自己的自定义验证来进行连接,但是最好使用before_validation ,因为这是before_validation的。

The model: 该模型:

class User < ActiveRecord::Base
  attr_accessible :group_name

  validates :group_name, :uniqueness => { :case_sensitive => false}

  before_validation :add_to_group_name

  protected
  def add_to_group_name
    self.group_name = group_name + '_Marc' # Edit however you want to here
  end
end

If you need to check the database for multiple values, you won't be able to with just a uniqueness validation, so you'll have to write a custom validation that searches the database for you. 如果需要检查数据库中是否有多个值,则不能仅使用唯一性验证,因此必须编写一个自定义验证来为您搜索数据库。 For your use case, it looks like you could use a like query: 对于您的用例,看起来您可以使用like查询:

# % is a wildcard character and matches anything
# So group_name + "%" will match anything starting with group_name
User.where("group_name like ?", group_name + "%") 

So your model would have a custom validation: 因此,您的模型将具有自定义验证:

class User < ActiveRecord::Base
  attr_accessible :group_name

  validate :check_group_name_uniqueness

  def check_group_name_uniqueness
    if User.where("group_name like ?", group_name + "%").count > 0
      errors.add(:group_name, "is already taken")
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM