noticed icon indicating copy to clipboard operation
noticed copied to clipboard

Thoughts on using MODEL_id instead of MODEL as param

Open henryaj opened this issue 2 years ago • 9 comments

Seems like noticed serializes the entire param in the Notification record. I have some pretty chunky models; I don't really want to save a whole copy of them within each Notification.

In some parts of my codebase, I've taken to using e.g. params :lender_id instead of params :lender, and then just doing a Lender.find on the param when I need to get the Lender record back. This seems like it would be fine in quite a lot of circumstances.

Is there any thought on which is preferred and why? I don't think has_noticed_notification works if using MODEL_id, but it seems like it would be a small change for it to be supported (which I could potentially make).

henryaj avatar Mar 25 '22 14:03 henryaj

I'm fine with adding it as long as it's not a breaking change. Any idea how you envision that working?

excid3 avatar Mar 25 '22 21:03 excid3

Something like has_noticed_notifications, reference: true or similar?

henryaj avatar Mar 29 '22 14:03 henryaj

Hmm, I feel like we could be more descriptive. by_id: true or something?

Or maybe it needs to be a new method?

has_noticed_notifications_by_id

I'm not sure.

excid3 avatar Mar 29 '22 14:03 excid3

Would it make sense to use the model's Global ID instead - that way there could still be some magic in converting that data back to an ActiveRecord instance if required?

dwightwatson avatar Apr 21 '22 10:04 dwightwatson

@dwightwatson That's already how it works using the ActiveJob coder.

I don't think GlobalID would help for this stuff because the key is already going to denote the model name.

excid3 avatar Apr 21 '22 13:04 excid3

I made this work in my project by adding a polymorphic subject field to the notifications table. Feels like it could be upstreamed. Improves the AR has_many_notifications logic a bit and removes the need to do a query on a jsonb field to get your associated notifications.

create_table :notifications do |t|
  t.references :recipient, polymorphic: true, null: false
  t.references :subject, polymorphic: true
  t.string :type, null: false
  t.jsonb :params
  t.datetime :read_at

  t.timestamps
end
    
class Notification < ApplicationRecord
  include Noticed::Model
  belongs_to :recipient, polymorphic: true
  belongs_to :subject, polymorphic: true
end

class MessageNotification < Noticed::Base
  deliver_by :database, format: :format_database
  
  def format_database
    {
      type: self.class.name,
      params: params,
      subject: params[:message]
    }
  end
end

class Message < ApplicationRecord
  has_many :notifications, as: :subject, dependent: :destroy
end

marcoadkins avatar May 18 '22 20:05 marcoadkins

We are trying to use it with a multi-tenant system, with ros_apartment gem, and it errors out when deserializing user with globalid, i am not fully sure how globalids work in rails/ruby, and if it makes DB request(if it does, it will fail obv and tenant needs to be set to find anything in User table, which can only set after job is running(using serializer or anything, not when params deserialisation is going on).

Any quick solution for our scenario?

ziaulrehman40 avatar Jul 27 '22 10:07 ziaulrehman40

I think we just HAVE to avoid globalids and play on plain ids, we will try some monkey patching for now

ziaulrehman40 avatar Jul 27 '22 10:07 ziaulrehman40

UPDATE: We are going en-route to send in ids instead of objects in recipients and overriding methods needed(mainly in the delivery_methods we are using, so far database and email had to be overriden)

ziaulrehman40 avatar Jul 29 '22 09:07 ziaulrehman40

I have the same issue, and wondered if a very simple solution could an option to specify where to get the value from, e.g.:

has_noticed_notifications param_name: "model_id", param_method: :id

Will happily create a PR for this :-)

AndersGM avatar Feb 17 '23 13:02 AndersGM

+1

MrKirat avatar Oct 10 '23 11:10 MrKirat

Hello, @excid3 ! Happy to see a new version of this gem! Could you please share if the suggested functionality from this issue has been implemented since I can't find anything about it in the new gem's documentation? Thank you!

MrKirat avatar Jan 16 '24 09:01 MrKirat

Yep, see the part about the record param as special and gets saved as the record:belongs_to{polymorphic} association on the Noticed::Event.

excid3 avatar Jan 16 '24 12:01 excid3

@excid3 Glad the polymorphic idea panned out. Been working well for us for over a year now.

marcoadkins avatar Jan 16 '24 18:01 marcoadkins