You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an interesting issue. We are implementing sharding in our data and it is causing issues for soft-deleted data. The major issue is polymorphic relationships while trying to calculate the shard key.
For example...
class Question < ActiveRecord::Base
acts_as_paranoid
belongs_to :questionable, polymorphic: true
before_save :calculate_company_id, if: :questionable_id_changed?
def calculate_company_id #shard key
self.company_id = self.try(:questionable).try(:company_id)
end
end
class Survey < ActiveRecord::Base
acts_as_paranoid
belongs_to :company
has_many :questions, as: :questionable
end
class Quiz < ActiveRecord::Base
acts_as_paranoid
belongs_to :company
has_many :questions, as: :questionable
end
If we have a soft deleted Quiz and a Question that is associated with that soft deleted Quiz and you call the calculate_company_id method, then nil is returned. In order to get this to work, adding unscoped around the code in question self.company_id = self.try(:questionable).try(:company_id) will solve this, but you need nested unscoped blocks. For example...
class Question < ActiveRecord::Base
acts_as_paranoid
belongs_to :questionable, polymorphic: true
before_save :calculate_company_id, if: :questionable_id_changed?
def calculate_company_id #shard key
Quiz.unscoped do
Survey.unscoped do
self.company_id = self.try(:questionable).try(:company_id)
end
end
end
end
so that it works for both related object types. Is there a way to do this without having to nest the blocks and still accomplish the same outcoming? Thanks for any help!
The text was updated successfully, but these errors were encountered:
I have an interesting issue. We are implementing sharding in our data and it is causing issues for soft-deleted data. The major issue is polymorphic relationships while trying to calculate the shard key.
For example...
If we have a soft deleted
Quiz
and aQuestion
that is associated with that soft deletedQuiz
and you call thecalculate_company_id
method, then nil is returned. In order to get this to work, adding unscoped around the code in questionself.company_id = self.try(:questionable).try(:company_id)
will solve this, but you need nested unscoped blocks. For example...so that it works for both related object types. Is there a way to do this without having to nest the blocks and still accomplish the same outcoming? Thanks for any help!
The text was updated successfully, but these errors were encountered: