Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Luke Griffiths has given you most of the answer. The essence is this, you have a uniqueness validation somewhere. According to your comment the only one you have is this one:</p> <pre><code>validates_uniqueness_of :body, :scope =&gt; [:commentable_id, :user_id], :message =&gt; "Duplicate record.." </code></pre> <p>If that is the case, then we must assume it is the culprit (it would help to know which model that validation was, also would help to see the relevant parts of your User model code as well as the relevant parts of the model where that validation is found). But no matter we can still have a guess as to what your problem is.</p> <p>The issue you're getting occurs in the <code>build_relation</code> method in the rails source <code>rails/activerecord/lib/active_record/validations/uniqueness.rb</code> on line 57. This method is trying to look up a column based on the name of an attribute and then tries to see if it is text. The errors occurs because the attribute it is getting does not seem to be in the column hash to the value of <code>column</code> is nil.</p> <p>It has been suggested in another answer that you output some debugging info in the build_relation method. This is not a bad idea (all you really need to know is what are the keys in the columns_hash and what the the value of the attribute that is passed in to the method), here is how you do that. Create an initializer in <code>config/initializers</code>, lets call it <code>debugging_stuff.rb</code>. Inside there you can put the following:</p> <pre><code>ActiveRecord::Validations::UniquenessValidator.class_eval do def build_relation(klass, table, attribute, value) #:nodoc: puts "************** #{attribute}" puts "************** #{klass.columns_hash.keys.inspect}" column = klass.columns_hash[attribute.to_s] value = column.limit ? value.to_s.mb_chars[0, column.limit] : value.to_s if column.text? if !options[:case_sensitive] &amp;&amp; value &amp;&amp; column.text? # will use SQL LOWER function before comparison relation = table[attribute].lower.eq(table.lower(value)) else value = klass.connection.case_sensitive_modifier(value) relation = table[attribute].eq(value) end relation end end </code></pre> <p>What we're looking for is to find out when none of the keys in the <code>columns_hash</code> match the attribute value that is passed.</p> <p>If that uniqueness validator above is really the only one you have, it should be one of the three attributes in there (<code>:body, :commentable_id, :user_id</code>). My money is on <code>:body</code> but I could be wrong. Does <code>:body</code> refer to another model. If it does perhaps it should be <code>:body_id</code> as Luke Griffiths indicated. If you still can't find the issue, you would need to provide more debugging info like, full stack trace and code for the relevant models (User etc.) - we've gone as far as we can with the information we have.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload