hzamani / acts_as_relation

Multi table Inheritance for rails
http://hzamani.github.com/acts_as_relation/
MIT License
180 stars 58 forks source link

Only forward attr_accessible if needed #24

Closed schuetzm closed 11 years ago

schuetzm commented 11 years ago

attr_accessible has been deprecated in Rails 4 in favour of strong parameters, and extracted to a gem. Attempting to use it without this gem results in the following error:

RuntimeError:attr_accessibleis extracted out of Rails into a gem. Please use new recommended protection model for params(strong_parameters) or addprotected_attributesto your Gemfile to use old one.

This change fixes that by only calling attr_accessible in Rails versions < 4 or when the protected_attributes gem is loaded.

jocubeit commented 11 years ago

Nice

schuetzm commented 11 years ago

I made another change, because the :conditions and :include options are deprecated too.

At some point, there should probably be support for passing arbitrary scope operations (i.e. where, order etc.), but doing so would require using non-final default arguments (that how has_one does it), which aren't available in Ruby 1.8. So this should probably be done when acts_as_relation decides it no longer supports older versions.

hzamani commented 11 years ago

I don't like version checking in code. I prefer to make a branch for Rails 4, so it will not have Ruby support problems. Any Idea?

schuetzm commented 11 years ago

Sure, it's easy to remove the version checks. Then where and the like can be implemented too. Do you want me to rewrite the patch?

schuetzm commented 11 years ago

Ok, I've removed the version checks and made some other changes for Rails 4. :conditions and :include are no longer supported; they generate deprecation warnings and are otherwise ignored. The documentation has been updated accordingly.

hzamani commented 11 years ago

Great work, this will be merged soon.

bigardone commented 11 years ago

Hi! I've updated my app to Rails4. Before updating it, the models that were using your gem worked fine. After upgrading to Rails4 and discovering this pull request, I changed my gemfile in order to use @schuetzm Rails4 branch, but I keep having this errors in every model that uses acts_as:

> AsignacionInmueble.first

   AsignacionInmueble.first
DEPRECATION WARNING: The following options in your AsignacionInmueble.has_one :notificacion declaration are deprecated: :include,:conditions. Please use a scope block instead. For example, the following:

    has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'

should be rewritten as the following:

    has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'
. (called from <class:AsignacionInmueble> at /Users/myuser/Documents/projects/project/MyProject/MyGemset/app/models/asignacion_inmueble.rb:2)
DEPRECATION WARNING: Calling #default_scope without a block is deprecated. For example instead of `default_scope where(color: 'red')`, please use `default_scope { where(color: 'red') }`. (Alternatively you can just redefine self.default_scope.). (called from acts_as at (eval):1)
DEPRECATION WARNING: Calling #default_scope without a block is deprecated. For example instead of `default_scope where(color: 'red')`, please use `default_scope { where(color: 'red') }`. (Alternatively you can just redefine self.default_scope.). (called from acts_as at (eval):1)
ArgumentError: Unknown key: include
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activesupport-4.0.0.beta1/lib/active_support/core_ext/hash/keys.rb:70:in `block in assert_valid_keys'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activesupport-4.0.0.beta1/lib/active_support/core_ext/hash/keys.rb:69:in `each_key'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activesupport-4.0.0.beta1/lib/active_support/core_ext/hash/keys.rb:69:in `assert_valid_keys'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-4.0.0.beta1/lib/active_record/relation/merger.rb:10:in `initialize'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-4.0.0.beta1/lib/active_record/relation/spawn_methods.rb:44:in `new'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-4.0.0.beta1/lib/active_record/relation/spawn_methods.rb:44:in `merge!'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-4.0.0.beta1/lib/active_record/relation/spawn_methods.rb:33:in `merge'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-deprecated_finders-0.0.3/lib/active_record/deprecated_finders/association_builder.rb:24:in `block in to_proc'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-4.0.0.beta1/lib/active_record/associations/join_dependency/join_association.rb:106:in `instance_exec'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-4.0.0.beta1/lib/active_record/associations/join_dependency/join_association.rb:106:in `block (2 levels) in join_to'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-4.0.0.beta1/lib/active_record/associations/join_dependency/join_association.rb:104:in `each'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-4.0.0.beta1/lib/active_record/associations/join_dependency/join_association.rb:104:in `block in join_to'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-4.0.0.beta1/lib/active_record/associations/join_dependency/join_association.rb:69:in `each'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-4.0.0.beta1/lib/active_record/associations/join_dependency/join_association.rb:69:in `each_with_index'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-4.0.0.beta1/lib/active_record/associations/join_dependency/join_association.rb:69:in `join_to'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/activerecord-4.0.0.beta1/lib/active_record/relation/query_methods.rb:854:in `block in build_joins'
... 30 levels...
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/spring-0.0.8/lib/spring/application_manager.rb:21:in `synchronize'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/spring-0.0.8/lib/spring/application_manager.rb:43:in `with_child'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/spring-0.0.8/lib/spring/application_manager.rb:62:in `run'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/spring-0.0.8/lib/spring/server.rb:47:in `serve'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/spring-0.0.8/lib/spring/server.rb:35:in `block in boot'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/spring-0.0.8/lib/spring/server.rb:35:in `loop'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/spring-0.0.8/lib/spring/server.rb:35:in `boot'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/spring-0.0.8/lib/spring/server.rb:15:in `boot'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/spring-0.0.8/lib/spring/client/start.rb:13:in `call'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/spring-0.0.8/lib/spring/client/command.rb:7:in `call'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/spring-0.0.8/lib/spring/client.rb:23:in `run'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/gems/spring-0.0.8/bin/spring:4:in `<top (required)>'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/bin/spring:23:in `load'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/bin/spring:23:in `<main>'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/bin/ruby_noexec_wrapper:14:in `eval'
  from /Users/myuser/.rvm/gems/ruby-2.0.0-p0@MyGemset/bin/ruby_noexec_wrapper:14:in `<main>'2.0.0-p0

Is there anything I'm missing?

My Gemfile looks like this:

gem 'acts_as_relation', github: 'schuetzm/acts_as_relation', branch: 'rails4'

Thank you very much in advance!

schuetzm commented 11 years ago

What is hidden behind those ... 30 levels ...? If it doesn't include acts_as_relation, it's unlikely the problem is in this gem.

From the looks of it, you're probably still using has_many :whatever, :include => :something somewhere. Replace this by has_many :whatever, -> { includes(:something) } (note the "s" in includes) and it should work fine.

bigardone commented 11 years ago

Hi! Now is working fine. Thanks and sorry for any inconvenience.

bigardone commented 11 years ago

Now from de rails console works fine. Whe I do a UsuarioBanco.all I see that the queries are executed and no errores are thrown.

2.0.0p0 :018 > UsuarioBanco.all
  UsuarioBanco Load (0.6ms)  SELECT "usuarios_bancos".* FROM "usuarios_bancos" INNER JOIN "users" ON "users"."usuario_id" = "usuarios_bancos"."id" AND "users"."usuario_type" = 'UsuarioBanco'
  User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."usuario_id" = $1 AND "users"."usuario_type" = $2 ORDER BY "users"."id" ASC LIMIT 1  [["usuario_id", 1], ["usuario_type", "UsuarioBanco"]]
 => #<ActiveRecord::Relation [#<UsuarioBanco id: 1, created_at: "2013-04-28 08:48:44", updated_at: "2013-04-28 08:48:44", banco_id: 1>]>

But when I use it in my controllers and views I get the following error:

NoMethodError - undefined method `result' for #<TypeError: nil is not a symbol>:
  (gem) activerecord-4.0.0.beta1/lib/active_record/connection_adapters/postgresql_adapter.rb:670:in `translate_exception'
  (gem) activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract_adapter.rb:338:in `rescue in log'
  (gem) activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract_adapter.rb:334:in `log'
  (gem) activerecord-4.0.0.beta1/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
  (gem) activerecord-4.0.0.beta1/lib/active_record/connection_adapters/postgresql_adapter.rb:861:in `column_definitions'
  (gem) activerecord-4.0.0.beta1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:135:in `columns'
  (gem) activerecord-4.0.0.beta1/lib/active_record/connection_adapters/schema_cache.rb:86:in `block in prepare_default_proc'
  (gem) activerecord-4.0.0.beta1/lib/active_record/model_schema.rb:208:in `columns'
  (gem) activerecord-4.0.0.beta1/lib/active_record/model_schema.rb:253:in `content_columns'
  /Users/user/.rvm/gems/ruby-2.0.0-p0@GemSet/bundler/gems/acts_as_relation-70f358b702f8/lib/active_record/acts_as_relation.rb:70:in `block (2 levels) in acts_as'
  /Users/user/.rvm/gems/ruby-2.0.0-p0@GemSet/bundler/gems/acts_as_relation-70f358b702f8/lib/active_record/acts_as_relation.rb:112:in `block in acts_as'
  /Users/user/.rvm/gems/ruby-2.0.0-p0@GemSet/bundler/gems/acts_as_relation-70f358b702f8/lib/active_record/acts_as_relation.rb:111:in `acts_as'
  app/models/usuario_banco.rb:2:in `<class:UsuarioBanco>'
  app/models/usuario_banco.rb:1:in `<top (required)>'
  (gem) activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:423:in `block in load_file'
  (gem) activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:615:in `new_constants_in'
  (gem) activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:422:in `load_file'
  (gem) activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:323:in `require_or_load'
  (gem) activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:462:in `load_missing_constant'
  (gem) activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:183:in `const_missing'
  (gem) activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:494:in `load_missing_constant'
  (gem) activesupport-4.0.0.beta1/lib/active_support/dependencies.rb:183:in `const_missing'
  app/controllers/home_controller.rb:3:in `index'
  (gem) actionpack-4.0.0.beta1/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  (gem) actionpack-4.0.0.beta1/lib/abstract_controller/base.rb:189:in `process_action'
  (gem) actionpack-4.0.0.beta1/lib/action_controller/metal/rendering.rb:10:in `process_action'
  (gem) actionpack-4.0.0.beta1/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  (gem) activesupport-4.0.0.beta1/lib/active_support/callbacks.rb:393:in `_run__1521953346924285959__process_action__callbacks'
  (gem) activesupport-4.0.0.beta1/lib/active_support/callbacks.rb:78:in `run_callbacks'
  (gem) actionpack-4.0.0.beta1/lib/abstract_controller/callbacks.rb:17:in `process_action'
  (gem) actionpack-4.0.0.beta1/lib/action_controller/metal/rescue.rb:29:in `process_action'
  (gem) actionpack-4.0.0.beta1/lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  (gem) activesupport-4.0.0.beta1/lib/active_support/notifications.rb:158:in `block in instrument'
  (gem) activesupport-4.0.0.beta1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  (gem) activesupport-4.0.0.beta1/lib/active_support/notifications.rb:158:in `instrument'
  (gem) actionpack-4.0.0.beta1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  (gem) actionpack-4.0.0.beta1/lib/action_controller/metal/params_wrapper.rb:245:in `process_action'

I'm not doing anything special in them:

 # app/controllers/home_controller.rb
 class HomeController < ApplicationController
   def index
     @users = UsuarioBanco.all
   end
 end

I can't understand the difference between why is giving me the error from the controller, and not from the rails console :(

schuetzm commented 11 years ago

This is a problem which is fixed in the current rails git (more specifically in commit e2a4b7a506e36839771284942829cbbe8d40b377). Try using gem 'rails', github: 'rails/rails' as a temporary workaround, until the next/final version is released.

bigardone commented 11 years ago

@schuetzm now it works like a charm! thank you so much for your help :)

abunsenonesale commented 11 years ago

Hey guys, any idea if this is going to get merged in?