Skip to content

Instantly share code, notes, and snippets.

@seanlinsley
Created April 17, 2013 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanlinsley/5408504 to your computer and use it in GitHub Desktop.
Save seanlinsley/5408504 to your computer and use it in GitHub Desktop.
Debugging for github.com/ernie/ransack/issues/173 and github.com/gregbell/active_admin/pull/1979
gem 'rails', '3.2.13'
gem 'ransack'
require 'rails/all'
require 'ransack'
ActiveRecord::Base.establish_connection adapter: 'sqlite3',
database: ':memory:'
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.integer :id
end
create_table :comments, force: true do |t|
t.integer :id
t.integer :commentable_id
t.string :commentable_type
end
end
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end
class User < ActiveRecord::Base
has_many :comments, as: :commentable
end
# ----------------------------------------------
# this works
Comment.search(commentable_type_cont:'e').result
# But when building a form:
form_for the_current_ransack_search_object do |f|
f.input :commentable_type
end
# `ActionView::Helpers::InstanceTag#value_before_type_cast` calls this:
object.respond_to? "commentable_type_cont_before_type_cast"
# and `Ransack::Search#respond_to?` calls this:
base.attribute_method? "commentable_type_cont_before_type_cast"
# which ends up calling `.klass` on the polymorphic association relfection,
# which returns an incorrect object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment