{"action":"created","issue":{"url":"https://api.github.com/repos/hzamani/active_record-acts_as/issues/27","labels_url":"https://api.github.com/repos/hzamani/active_record-acts_as/issues/27/labels{/name}","comments_url":"https://api.github.com/repos/hzamani/active_record-acts_as/issues/27/comments","events_url":"https://api.github.com/repos/hzamani/active_record-acts_as/issues/27/events","html_url":"https://github.com/hzamani/active_record-acts_as/issues/27","id":53543733,"number":27,"title":"Broken queries when used in conjunction with friendly_id and globalize","user":{"login":"lordofthelake","id":425406,"avatar_url":"https://avatars.githubusercontent.com/u/425406?v=3","gravatar_id":"","url":"https://api.github.com/users/lordofthelake","html_url":"https://github.com/lordofthelake","followers_url":"https://api.github.com/users/lordofthelake/followers","following_url":"https://api.github.com/users/lordofthelake/following{/other_user}","gists_url":"https://api.github.com/users/lordofthelake/gists{/gist_id}","starred_url":"https://api.github.com/users/lordofthelake/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lordofthelake/subscriptions","organizations_url":"https://api.github.com/users/lordofthelake/orgs","repos_url":"https://api.github.com/users/lordofthelake/repos","events_url":"https://api.github.com/users/lordofthelake/events{/privacy}","received_events_url":"https://api.github.com/users/lordofthelake/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-01-06T18:05:41Z","updated_at":"2015-01-06T21:33:43Z","closed_at":null,"body":"When using `acts_as` in conjunction with `friendly_id` and `globalize`, the finders on the model get broken due to an incorrect WHERE condition injected in the query (`parent_model.child_model_translations.slug = ?` where it should have been `child_model_translations.slug = ?`).\r\n\r\nModels:\r\n```ruby\r\nclass Collection < ActiveRecord::Base\r\n  extend FriendlyId\r\n\r\n  translates :name, :slug\r\n  friendly_id :name, use: :globalize\r\n  acts_as :box\r\nend\r\n\r\nclass Box < ActiveRecord::Base\r\n  default_scope -> { where(in_home: true) }\r\n  acts_as_list scope: [:in_home]\r\n  actable\r\nend\r\n```\r\n\r\nMy `schema.rb` (relevant portions):\r\n```ruby\r\n  create_table \"boxes\", force: true do |t|\r\n    t.integer  \"actable_id\"\r\n    t.string   \"actable_type\"\r\n    t.integer  \"position\",     default: 0\r\n    t.boolean  \"in_home\",      default: false\r\n    t.boolean  \"featured\",     default: false\r\n    t.datetime \"created_at\"\r\n    t.datetime \"updated_at\"\r\n  end\r\n\r\n  create_table \"collection_translations\", force: true do |t|\r\n    t.integer  \"collection_id\", null: false\r\n    t.string   \"locale\",        null: false\r\n    t.datetime \"created_at\"\r\n    t.datetime \"updated_at\"\r\n    t.string   \"name\"\r\n    t.string   \"slug\"\r\n  end\r\n\r\n  create_table \"collections\", force: true do |t|\r\n    t.string   \"name\"\r\n    t.string   \"icon\"\r\n    t.string   \"image\"\r\n    t.datetime \"created_at\"\r\n    t.datetime \"updated_at\"\r\n    t.string   \"color\"\r\n    t.string   \"slug\"\r\n  end\r\n```\r\n\r\nGem versions (from  `bundle show`)\r\n```\r\n * active_record-acts_as (1.0.2)\r\n * activerecord (4.1.8)\r\n * friendly_id (5.0.4)\r\n * friendly_id-globalize (1.0.0.alpha1 058f863)\r\n * globalize (4.0.3)\r\n```\r\n\r\nException raised when invoking, e.g., `Collection.friendly.find('autumn-winter')` or `Collection.find_by_slug('autumn-winter')`:\r\n\r\n```\r\nPG::UndefinedColumn: ERROR:  column boxes.collection_translations.slug does not exist\r\nLINE 1: ... = 'Collection' AND \"boxes\".\"in_home\" = 't' WHERE \"boxes\".\"c...\r\n                                                             ^\r\n: SELECT  \"collections\".\"id\" AS t0_r0, \"collections\".\"name\" AS t0_r1, \"collections\".\"icon\" AS t0_r2, \"collections\".\"image\" AS t0_r3, \"collections\".\"created_at\" AS t0_r4, \"collections\".\"updated_at\" AS t0_r5, \"collections\".\"color\" AS t0_r6, \"collections\".\"slug\" AS t0_r7, \"boxes\".\"id\" AS t1_r0, \"boxes\".\"actable_id\" AS t1_r1, \"boxes\".\"actable_type\" AS t1_r2, \"boxes\".\"position\" AS t1_r3, \"boxes\".\"in_home\" AS t1_r4, \"boxes\".\"featured\" AS t1_r5, \"boxes\".\"created_at\" AS t1_r6, \"boxes\".\"updated_at\" AS t1_r7 FROM \"collections\" INNER JOIN \"collection_translations\" ON \"collection_translations\".\"collection_id\" = \"collections\".\"id\" LEFT OUTER JOIN \"boxes\" ON \"boxes\".\"actable_id\" = \"collections\".\"id\" AND \"boxes\".\"actable_type\" = 'Collection' AND \"boxes\".\"in_home\" = 't' WHERE \"boxes\".\"collection_translations.slug\" = 'autumn-winter' AND \"collection_translations\".\"locale\" = 'en' LIMIT 1\r\nActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column boxes.collection_translations.slug does not exist\r\nLINE 1: ... = 'Collection' AND \"boxes\".\"in_home\" = 't' WHERE \"boxes\".\"c...\r\n                                                             ^\r\n: SELECT  \"collections\".\"id\" AS t0_r0, \"collections\".\"name\" AS t0_r1, \"collections\".\"icon\" AS t0_r2, \"collections\".\"image\" AS t0_r3, \"collections\".\"created_at\" AS t0_r4, \"collections\".\"updated_at\" AS t0_r5, \"collections\".\"color\" AS t0_r6, \"collections\".\"slug\" AS t0_r7, \"boxes\".\"id\" AS t1_r0, \"boxes\".\"actable_id\" AS t1_r1, \"boxes\".\"actable_type\" AS t1_r2, \"boxes\".\"position\" AS t1_r3, \"boxes\".\"in_home\" AS t1_r4, \"boxes\".\"featured\" AS t1_r5, \"boxes\".\"created_at\" AS t1_r6, \"boxes\".\"updated_at\" AS t1_r7 FROM \"collections\" INNER JOIN \"collection_translations\" ON \"collection_translations\".\"collection_id\" = \"collections\".\"id\" LEFT OUTER JOIN \"boxes\" ON \"boxes\".\"actable_id\" = \"collections\".\"id\" AND \"boxes\".\"actable_type\" = 'Collection' AND \"boxes\".\"in_home\" = 't' WHERE \"boxes\".\"collection_translations.slug\" = 'autumn-winter' AND \"collection_translations\".\"locale\" = 'en' LIMIT 1\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `async_exec'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `block in exec_no_cache'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `exec_no_cache'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/postgresql_adapter.rb:954:in `select'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/relation/finder_methods.rb:347:in `find_with_associations'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/relation.rb:611:in `exec_queries'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/relation.rb:493:in `load'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/relation.rb:238:in `to_a'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/relation/finder_methods.rb:460:in `find_take'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/relation/finder_methods.rb:98:in `take'\r\n... 6 levels...\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/console.rb:9:in `start'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:69:in `console'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:40:in `run_command!'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands.rb:17:in `<top (required)>'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `block in require'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'\r\n\tfrom /vagrant/bin/rails:8:in `<top (required)>'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:241:in `load'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:241:in `block in load'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:241:in `load'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'\r\n\tfrom /opt/rbenv/versions/2.1.5/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'\r\n\tfrom -e:1:in `<main>'\r\n```\r\n\r\nFull query that triggered the exception:\r\n```sql\r\nSELECT  \"collections\".\"id\" AS t0_r0, \"collections\".\"name\" AS t0_r1, \"collections\".\"icon\" AS t0_r2, \"collections\".\"image\" AS t0_r3, \"collections\".\"created_at\" AS t0_r4, \"collections\".\"updated_at\" AS t0_r5, \"collections\".\"color\" AS t0_r6, \"collections\".\"slug\" AS t0_r7, \"boxes\".\"id\" AS t1_r0, \"boxes\".\"actable_id\" AS t1_r1, \"boxes\".\"actable_type\" AS t1_r2, \"boxes\".\"position\" AS t1_r3, \"boxes\".\"in_home\" AS t1_r4, \"boxes\".\"featured\" AS t1_r5, \"boxes\".\"created_at\" AS t1_r6, \"boxes\".\"updated_at\" AS t1_r7 FROM \"collections\" INNER JOIN \"collection_translations\" ON \"collection_translations\".\"collection_id\" = \"collections\".\"id\" LEFT OUTER JOIN \"boxes\" ON \"boxes\".\"actable_id\" = \"collections\".\"id\" AND \"boxes\".\"actable_type\" = 'Collection' AND \"boxes\".\"in_home\" = 't' WHERE \"boxes\".\"collection_translations.slug\" = 'autumn-winter' AND \"collection_translations\".\"locale\" = 'en' LIMIT 1\r\n```\r\n\r\nWhat the query looks like without `acts_as`:\r\n```sql\r\nSELECT  \"collections\".* FROM \"collections\" INNER JOIN \"collection_translations\" ON \"collection_translations\".\"collection_id\" = \"collections\".\"id\" WHERE \"collection_translations\".\"slug\" = 'autumn-winter' AND \"collection_translations\".\"locale\" = 'en' LIMIT 1\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/hzamani/active_record-acts_as/issues/comments/68937796","html_url":"https://github.com/hzamani/active_record-acts_as/issues/27#issuecomment-68937796","issue_url":"https://api.github.com/repos/hzamani/active_record-acts_as/issues/27","id":68937796,"user":{"login":"lordofthelake","id":425406,"avatar_url":"https://avatars.githubusercontent.com/u/425406?v=3","gravatar_id":"","url":"https://api.github.com/users/lordofthelake","html_url":"https://github.com/lordofthelake","followers_url":"https://api.github.com/users/lordofthelake/followers","following_url":"https://api.github.com/users/lordofthelake/following{/other_user}","gists_url":"https://api.github.com/users/lordofthelake/gists{/gist_id}","starred_url":"https://api.github.com/users/lordofthelake/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lordofthelake/subscriptions","organizations_url":"https://api.github.com/users/lordofthelake/orgs","repos_url":"https://api.github.com/users/lordofthelake/repos","events_url":"https://api.github.com/users/lordofthelake/events{/privacy}","received_events_url":"https://api.github.com/users/lordofthelake/received_events","type":"User","site_admin":false},"created_at":"2015-01-06T21:33:43Z","updated_at":"2015-01-06T21:33:43Z","body":"Seems related to the same issue encountered by @kidlab in #9.\r\n\r\nMy first guess is that something is going awry in `lib/active_record/acts_as/querying.rb`, where the `ActiveRecord::QueryMethods#where` override is performed. \r\n\r\nI haven't fully made sense of the code yet, but my general impression is that the keys that should get prefixed don't get separated correctly from those that should remain instead unprefixed (lines 6 to 8)."}}