--- !ruby/object:RI::MethodDescription 
aliases: []

block_params: 
comment: 
- !ruby/struct:SM::Flow::P 
  body: Validates whether the value of the specified attributes are unique across the system. Useful for making sure that only one user can be named &quot;davidhh&quot;.
- !ruby/struct:SM::Flow::VERB 
  body: "  class Person &lt; ActiveRecord::Base\n    validates_uniqueness_of :user_name, :scope =&gt; :account_id\n  end\n"
- !ruby/struct:SM::Flow::P 
  body: It can also validate whether the value of the specified attributes are unique based on multiple scope parameters. For example, making sure that a teacher can only be on the schedule once per semester for a particular class.
- !ruby/struct:SM::Flow::VERB 
  body: "  class TeacherSchedule &lt; ActiveRecord::Base\n    validates_uniqueness_of :teacher_id, :scope =&gt; [:semester_id, :class_id]\n  end\n"
- !ruby/struct:SM::Flow::P 
  body: When the record is created, a check is performed to make sure that no record exists in the database with the given value for the specified attribute (that maps to a column). When the record is updated, the same check is made but disregarding the record itself.
- !ruby/struct:SM::Flow::P 
  body: "Configuration options:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: "<tt>:message</tt> - Specifies a custom error message (default is: &quot;has already been taken&quot;)."
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:scope</tt> - One or more columns by which to limit the scope of the uniqueness constraint.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:case_sensitive</tt> - Looks for an exact match. Ignored by non-text columns (<tt>true</tt> by default).
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is <tt>nil</tt> (default is <tt>false</tt>).
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is <tt>false</tt>).
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should occur (e.g. <tt>:if =&gt; :allow_validation</tt>, or <tt>:if =&gt; Proc.new { |user| user.signup_step &gt; 2 }</tt>). The method, proc or string should return or evaluate to a true or false value.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should not occur (e.g. <tt>:unless =&gt; :skip_validation</tt>, or <tt>:unless =&gt; Proc.new { |user| user.signup_step &lt;= 2 }</tt>). The method, proc or string should return or evaluate to a true or false value.
  type: :BULLET
- !ruby/struct:SM::Flow::H 
  level: 3
  text: Concurrency and integrity
- !ruby/struct:SM::Flow::P 
  body: "Using this validation method in conjunction with ActiveRecord::Base#save does not guarantee the absence of duplicate record insertions, because uniqueness checks on the application level are inherently prone to race conditions. For example, suppose that two users try to post a Comment at the same time, and a Comment's title must be unique. At the database-level, the actions performed by these users could be interleaved in the following manner:"
- !ruby/struct:SM::Flow::VERB 
  body: "              User 1                 |               User 2\n ------------------------------------+--------------------------------------\n # User 1 checks whether there's     |\n # already a comment with the title  |\n # 'My Post'. This is not the case.  |\n SELECT * FROM comments              |\n WHERE title = 'My Post'             |\n                                     |\n                                     | # User 2 does the same thing and also\n                                     | # infers that his title is unique.\n                                     | SELECT * FROM comments\n                                     | WHERE title = 'My Post'\n                                     |\n # User 1 inserts his comment.       |\n INSERT INTO comments                |\n (title, content) VALUES             |\n ('My Post', 'hi!')                  |\n                                     |\n                                     | # User 2 does the same thing.\n                                     | INSERT INTO comments\n                                     | (title, content) VALUES\n                                     | ('My Post', 'hello!')\n                                     |\n                                     | # ^^^^^^\n                                     | # Boom! We now have a duplicate\n                                     | # title!\n"
- !ruby/struct:SM::Flow::P 
  body: "This could even happen if you use transactions with the 'serializable' isolation level. There are several ways to get around this problem:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: "-"
    body: By locking the database table before validating, and unlocking it after saving. However, table locking is very expensive, and thus not recommended.
  - !ruby/struct:SM::Flow::LI 
    label: "-"
    body: By locking a lock file before validating, and unlocking it after saving. This does not work if you've scaled your Rails application across multiple web servers (because they cannot share lock files, or cannot do that efficiently), and thus not recommended.
  - !ruby/struct:SM::Flow::LI 
    label: "-"
    body: Creating a unique index on the field, by using ActiveRecord::ConnectionAdapters::SchemaStatements#add_index. In the rare case that a race condition occurs, the database will guarantee the field's uniqueness.
  - !ruby/struct:SM::Flow::P 
    body: "When the database catches such a duplicate insertion, ActiveRecord::Base#save will raise an ActiveRecord::StatementInvalid exception. You can either choose to let this error propagate (which will result in the default Rails exception page being shown), or you can catch it and restart the transaction (e.g. by telling the user that the title already exists, and asking him to re-enter the title). This technique is also known as optimistic concurrency control: http://en.wikipedia.org/wiki/Optimistic_concurrency_control"
  - !ruby/struct:SM::Flow::P 
    body: Active Record currently provides no way to distinguish unique index constraint errors from other types of database errors, so you will have to parse the (database-specific) exception message to detect such a case.
  type: :BULLET
full_name: ActiveRecord::Validations::ClassMethods#validates_uniqueness_of
is_singleton: false
name: validates_uniqueness_of
params: (*attr_names)
visibility: public
