belongs_to :commented, :polymorphic => true, :counter_cache => true
belongs_to :author, :class_name => 'User'
- validates_presence_of :commented, :author, :comments
+ validates_presence_of :commented, :author, :content
after_create :send_notification
safe_attributes 'comments'
+ def comments=(arg)
+ self.content = arg
+ end
+
+ def comments
+ content
+ end
+
private
def send_notification
end
end
class Relation ; undef open ; end
-
- # Workaround for a Rails 5.1 regression that breaks queries with a condition
- # on a table that has a column with the same name as the table
- # (eg. comments.comments). It breaks has_many associations to these tables as well.
- # https://github.com/rails/rails/commit/c6a62dc327c54baec87306f5c381e13cacc00a19
- #
- # Examples (without the following workaround applied):
- # Comment.where(:comments => {:id => 1})
- # TypeError: can't cast Hash
- #
- # News.first.comments.count
- # TypeError: can't cast Hash
- class PredicateBuilder
-
- protected
- alias :create_binds_for_hash_without_comments_fix :create_binds_for_hash
- def create_binds_for_hash(attributes)
- if attributes["comments"].is_a?(Hash)
- return create_binds_for_hash_without_comments_fix attributes["comments"]
- else
- create_binds_for_hash_without_comments_fix attributes
- end
- end
- end
end
module ActionView
--- /dev/null
+class RenameCommentsToContent < ActiveRecord::Migration[5.1]
+ def change
+ rename_column :comments, :comments, :content
+ end
+end
commented_id: 1
id: 1
author_id: 1
- comments: my first comment
+ content: my first comment
created_on: 2006-12-10 18:10:10 +01:00
updated_on: 2006-12-10 18:10:10 +01:00
comments_002:
commented_id: 1
id: 2
author_id: 2
- comments: This is an other comment
+ content: This is an other comment
created_on: 2006-12-10 18:12:10 +01:00
updated_on: 2006-12-10 18:12:10 +01:00