activerecord-multi-tenant
activerecord-multi-tenant copied to clipboard
after_initialize issue with partial arel `select`
a ActiveModel::MissingAttributeError is raised in the model_extensions.rb after_initialize
block if the partition_key
is not included in the custom select
was
# New instances should have the tenant set
after_initialize Proc.new { |record|
if MultiTenant.current_tenant_id && record.public_send(partition_key.to_sym).nil?
record.public_send("#{partition_key}=".to_sym, MultiTenant.current_tenant_id)
end
}
This can be fixed by:
- editing app code to include partition_key in the select
- checking that the partition_key is loaded before the public_send, eg
# New instances should have the tenant set
after_initialize Proc.new { |record|
if MultiTenant.current_tenant_id &&
(!record.attribute_present?(partition_key) || record.public_send(partition_key.to_sym).nil?)
record.public_send("#{partition_key}=".to_sym, MultiTenant.current_tenant_id)
end
}
(this ought to work for cases of partition_key-not-being-a-column, since attribute_present?
returns false for nonsense values, circa Rails 4.2.7.1)
3) add a method that rescues ActiveModel::MissingAttributeError while trying the public_send (in case there's some unmanageable override/alias preventing an attribute_present?
check.
Do you want a PR for option 2?
@stevenjonescgm I think a PR for option 2 would be great - thanks for finding & working on this!
hmm, unfortunately this change is insufficient as once write_attribute
is called, changes
is then {"account_id"=>[#<Object:0x00007f81fe9609d8>, 1]}
I don't yet know where that generic object is coming from, though it seems to be a change from Rails 4.2 to Rails 5
I may just cache the previous value in a local variable for the nil check if I don't track down that anonymous Object
also btw your Rails 4.0 and 4.1 specs, eg bundle exec appraisal rails-4.0 rake
have
undefined method `visit_Integer`
errors
hah, 17 days ago Rails 5-2-stable (and Rails 6) merged https://github.com/rails/rails/pull/31698 I modified my environment to test 5.2.0.rc1 and saw the same behaviors (on MRI 2.4.2) despite the refactor.
@stevenjonescgm is this still an issue. If so, could you add some tests?
Fixed in #35