has_many dependent:destroy throw'错误的参数个数(0表示1)'

时间:2014-10-16 13:51:57

标签: ruby-on-rails ruby activerecord

所以我的应用程序中有用户,媒体和评论。 用户和媒体都有 has_many :comments。 评论模型对用户和媒体都有belongs_to

现在,在用户和媒体上的has_many之后,我也有dependent: destroy选项。

现在当我进入用户的索引页面时,一切都很好但是当我进入媒体的索引页面时,它会在has_many方法上抛出错误:wrong number of arguments (0 for 1)

所有模型都有相应的控制器,并且定义了相应的方法,如destroy方法等。

这可能是什么问题?

媒体模式:

class Media < ActiveRecord::Base
    belongs_to :user
    before_create :randomize_file_name

    private
        def randomize_file_name
            extension = File.extname(image_video_file_name).downcase
            self.image_video.instance_write(:file_name, "#{SecureRandom.hex(8)}#{extension}")
        end

    has_many :comments, dependent: destroy
end

用户模型:

class User < ActiveRecord::Base
    validates :name, presence: true, length: {maximum: 50}

    enum role: [:user, :moderator, :admin]
    after_initialize :set_default_role, :if => :new_record?

    def set_default_role
        self.role ||= :user
    end

    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable

    has_many :medias, dependent: :destroy
    has_many :comments, dependent: :destroy
end

评论模型:

class Comment < ActiveRecord::Base
    belongs_to :user
    belongs_to :media

    validates :text, presence: true, length: { maximum: 255 }
    validates :user_id, presence: true
    validates :media_id, presence: true
end

完整错误:

ArgumentError - wrong number of arguments (0 for 1):
  activerecord (4.1.6) lib/active_record/querying.rb:8:in `destroy'
  app/models/media.rb:19:in `<class:Media>'
  app/models/media.rb:1:in `<top (required)>'
  activesupport (4.1.6) lib/active_support/dependencies.rb:443:in `block in load_file'
  activesupport (4.1.6) lib/active_support/dependencies.rb:633:in `new_constants_in'
  activesupport (4.1.6) lib/active_support/dependencies.rb:442:in `load_file'
  activesupport (4.1.6) lib/active_support/dependencies.rb:342:in `require_or_load'
  activesupport (4.1.6) lib/active_support/dependencies.rb:480:in `load_missing_constant'
  activesupport (4.1.6) lib/active_support/dependencies.rb:180:in `const_missing'
  activesupport (4.1.6) lib/active_support/dependencies.rb:512:in `load_missing_constant'
  activesupport (4.1.6) lib/active_support/dependencies.rb:180:in `const_missing'
  app/controllers/media_controller.rb:5:in `index'
  actionpack (4.1.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.1.6) lib/abstract_controller/base.rb:189:in `process_action'
  actionpack (4.1.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.1.6) lib/active_support/callbacks.rb:113:in `call'
  activesupport (4.1.6) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
  activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `block in halting'
  activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `block in halting'
  activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.6) lib/active_support/callbacks.rb:86:in `run_callbacks'
  actionpack (4.1.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.1.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.1.6) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.1.6) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.1.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.1.6) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.1.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.1.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.1.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.1.6) lib/abstract_controller/base.rb:136:in `process'
  actionview (4.1.6) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.1.6) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.1.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.1.6) lib/action_controller/metal.rb:232:in `block in action'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:50:in `call'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:73:in `block in call'
  actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:678:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/flash.rb:254:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.1.6) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
  activerecord (4.1.6) lib/active_record/migration.rb:380:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks'
  actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  better_errors (2.0.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.0.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.0.0) lib/better_errors/middleware.rb:57:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.6) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.0.3) lib/quiet_assets.rb:23:in `call_with_quiet_assets'
  actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.6) lib/rails/engine.rb:514:in `call'
  railties (4.1.6) lib/rails/application.rb:144:in `call'
  passenger (4.0.53) lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
  passenger (4.0.53) lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
  passenger (4.0.53) lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
  passenger (4.0.53) lib/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'

1 个答案:

答案 0 :(得分:7)

您的media课程中有拼写错误,:destroy需要成为符号:

class Media < ActiveRecord::Base
    belongs_to :user
    before_create :randomize_file_name

    private
        def randomize_file_name
            extension = File.extname(image_video_file_name).downcase
            self.image_video.instance_write(:file_name, "#{SecureRandom.hex(8)}#{extension}")
        end

    has_many :comments, dependent: :destroy
end