On Wed, Nov 17, 2021 at 11:58 PM [email protected] < [email protected]> wrote:
> Hi, > > I have a query like this: > > item = Product.select(:description, :info).where(x: 1).first > > Now, What I want to do is modify "description" and "info" fields before > sending it to the browser/client. Here is my attempt: > > class Product < Sequel::Model > def description > description + 'My modification at end' > end > > def info > 'override string' > end > end > > And when I test like this: > > puts item.info #> 'override string' > > I get correct modified value BUT problem is when I send this to browser > via Hash or JSON then original values are sent. I still get old values > instead of my modified values. > > How can I make my modifications and changes "permanent" when model is > converted to hash or json? > If you really want to make your changes permanent, you should update the database values for the columns, and remove the methods. However, I'm guessing there are reasons you don't want the changes to be that permanent, so you probably want to override to_json/to_h on the model. Here's an example for to_h: def to_h h = super h[:description] = description h[:info] = info h end Thanks, Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/CADGZSSdpbZViNU3g0qr5FAWJXzTFfDnOwjB_RV1YpgVGbC9heA%40mail.gmail.com.
