无法访问关联的只读模型的属性

时间:2016-06-08 22:48:47

标签: ruby-on-rails ruby friendly-id

我使用friendly_id gem生成slu ..

我有这样的模型

class Veterinarian < ApplicationRecord
  extend FriendlyId

  belongs_to :user
  has_one :profile, through: :user

  friendly_id :slug_string, use: :slugged

  def slug_string
    "#{profile.first_name} #{profile.last_name} #{profile.city}"
  end
end

另一个这样的模特。

class Profile < ApplicationRecord
  include ReadOnlyModel

  belongs_to :user
end

ReadOnlyModel看起来像这样

module ReadOnlyModel
  extend ActiveSupport::Concern

  included do
    attr_readonly(*column_names)
  end

  def readonly?
    true
  end

  def destroy
    raise ActiveRecord::ReadOnlyRecord
  end

  def delete
    raise ActiveRecord::ReadOnlyRecord
  end
end

当我尝试访问兽医模型中的个人资料属性时,我的问题就来了。 profile.first_nameprofile.last_name

当friendly_id尝试更新slug时,它会抛出一个错误,告诉我配置文件模型是只读的,但是我只是访问一个属性并使用它创建一个字符串以供friendly_id使用,没有任何变化在配置文件模型中。但是我可以在控制台中执行Veterinarian.last.slug_string并且输出正确且没有错误。

我通过执行Veterinarian.update(slug: nil)将slug设置为nil并触发friendly_id以使用正确的slug更新它来测试它。

我一直在摸不着头脑,并且无法弄清楚发生了什么。它可能是我忽视的简单事物。任何帮助将不胜感激。

我的上述模型包含在Veterinarians模块中,出于可读性原因,我在代码示例中删除了它。

activerecord (5.0.0.beta3) lib/active_record/persistence.rb:533:in `create_or_update'
activerecord (5.0.0.beta3) lib/active_record/callbacks.rb:298:in `block in create_or_update'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:750:in `_run_save_callbacks'
activerecord (5.0.0.beta3) lib/active_record/callbacks.rb:298:in `create_or_update'
activerecord (5.0.0.beta3) lib/active_record/suppressor.rb:41:in `create_or_update'
activerecord (5.0.0.beta3) lib/active_record/persistence.rb:125:in `save'
activerecord (5.0.0.beta3) lib/active_record/validations.rb:44:in `save'
activerecord (5.0.0.beta3) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (5.0.0.beta3) lib/active_record/transactions.rb:319:in `block (2 levels) in save'
activerecord (5.0.0.beta3) lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
activerecord (5.0.0.beta3) lib/active_record/connection_adapters/abstract/database_statements.rb:228:in `transaction'
activerecord (5.0.0.beta3) lib/active_record/transactions.rb:211:in `transaction'
activerecord (5.0.0.beta3) lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
activerecord (5.0.0.beta3) lib/active_record/transactions.rb:319:in `block in save'
activerecord (5.0.0.beta3) lib/active_record/transactions.rb:334:in `rollback_active_record_state!'
activerecord (5.0.0.beta3) lib/active_record/transactions.rb:318:in `save'
activerecord (5.0.0.beta3) lib/active_record/persistence.rb:266:in `block in update'
activerecord (5.0.0.beta3) lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
activerecord (5.0.0.beta3) lib/active_record/connection_adapters/abstract/database_statements.rb:230:in `block in transaction'
activerecord (5.0.0.beta3) lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
activerecord (5.0.0.beta3) lib/active_record/connection_adapters/abstract/database_statements.rb:230:in `transaction'
activerecord (5.0.0.beta3) lib/active_record/transactions.rb:211:in `transaction'
activerecord (5.0.0.beta3) lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
activerecord (5.0.0.beta3) lib/active_record/persistence.rb:264:in `update'
engines/practices/app/controllers/practices/clinic_veterinarians_controller.rb:13:in `show'
actionpack (5.0.0.beta3) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.0.beta3) lib/abstract_controller/base.rb:183:in `process_action'
actionpack (5.0.0.beta3) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.0.beta3) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.beta3) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.0.beta3) lib/action_controller/metal/rescue.rb:27:in `process_action'
actionpack (5.0.0.beta3) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (5.0.0.beta3) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.beta3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.beta3) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.0.beta3) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (5.0.0.beta3) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.0.beta3) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.0.beta3) lib/abstract_controller/base.rb:128:in `process'
actionview (5.0.0.beta3) lib/action_view/rendering.rb:30:in `process'
actionpack (5.0.0.beta3) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.0.beta3) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.0.beta3) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.0.beta3) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.0.beta3) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0.beta3) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0.beta3) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0.beta3) lib/action_dispatch/routing/route_set.rb:724:in `call'
railties (5.0.0.beta3) lib/rails/engine.rb:522:in `call'
railties (5.0.0.beta3) lib/rails/railtie.rb:194:in `public_send'
railties (5.0.0.beta3) lib/rails/railtie.rb:194:in `method_missing'
actionpack (5.0.0.beta3) lib/action_dispatch/routing/mapper.rb:18:in `block in <class:Constraints>'
actionpack (5.0.0.beta3) lib/action_dispatch/routing/mapper.rb:47:in `serve'
actionpack (5.0.0.beta3) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0.beta3) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0.beta3) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0.beta3) lib/action_dispatch/routing/route_set.rb:724:in `call'
omniauth (1.3.0) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.3.0) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.3.0) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.3.0) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.3.0) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.3.0) lib/omniauth/strategy.rb:164:in `call'
actionview (5.0.0.beta3) lib/action_view/digestor.rb:12:in `call'
/Users/mike/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/remotipart-aa0386690bd2/lib/remotipart/middleware.rb:27:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
/Users/mike/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/rack-1aded61cedb0/lib/rack/etag.rb:25:in `call'
/Users/mike/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/rack-1aded61cedb0/lib/rack/conditional_get.rb:25:in `call'
/Users/mike/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/rack-1aded61cedb0/lib/rack/head.rb:12:in `call'
/Users/mike/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/rack-1aded61cedb0/lib/rack/session/abstract/id.rb:222:in `context'
/Users/mike/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/rack-1aded61cedb0/lib/rack/session/abstract/id.rb:216:in `call'
actionpack (5.0.0.beta3) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.0.0.beta3) lib/active_record/query_cache.rb:36:in `call'
activerecord (5.0.0.beta3) lib/active_record/connection_adapters/abstract/connection_pool.rb:963:in `call'
actionpack (5.0.0.beta3) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0.beta3) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.beta3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (5.0.0.beta3) lib/action_dispatch/middleware/reloader.rb:71:in `call'
actionpack (5.0.0.beta3) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.0.beta3) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
actionpack (5.0.0.beta3) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0.beta3) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0.beta3) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0.beta3) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0.beta3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0.beta3) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0.beta3) lib/rails/rack/logger.rb:24:in `call'
request_store (1.3.1) lib/request_store/middleware.rb:9:in `call'
actionpack (5.0.0.beta3) lib/action_dispatch/middleware/request_id.rb:24:in `call'
/Users/mike/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/rack-1aded61cedb0/lib/rack/method_override.rb:22:in `call'
/Users/mike/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/rack-1aded61cedb0/lib/rack/runtime.rb:22:in `call'
actionpack (5.0.0.beta3) lib/action_dispatch/middleware/load_interlock.rb:13:in `call'
actionpack (5.0.0.beta3) lib/action_dispatch/middleware/static.rb:136:in `call'
/Users/mike/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/rack-1aded61cedb0/lib/rack/sendfile.rb:111:in `call'
railties (5.0.0.beta3) lib/rails/engine.rb:522:in `call'
puma (2.16.0) lib/puma/server.rb:557:in `handle_request'
puma (2.16.0) lib/puma/server.rb:404:in `process_client'
puma (2.16.0) lib/puma/server.rb:270:in `block in run'
puma (2.16.0) lib/puma/thread_pool.rb:106:in `block in spawn_thread'

1 个答案:

答案 0 :(得分:0)

进一步挖掘后,似乎执行user.profile.attribute_name不会触发只读错误。