On Tuesday, July 30, 2019 at 1:23:35 PM UTC-7, Jason Landry wrote:
>
> I have an interesting (well, to me anyway LOL) and perhaps philosophical 
> issue with the :dirty plug and using a model instance with a join.
>
> Consider the following overly simplified example:  Get the ID of one order 
> from today, and the customer's name by joining it.
>
>
> Order.plugin :dirty
>
> one_order = Order
>  .where(date: Date.today)
>  .join(:customers, id: :customer_id)
>  .select(:id, :customers__name)
>  .first
>
> puts one_order[:name] 
> # "Bob Johnson" -- customer name
>
>
> This works fine.  `one_order` is technically an `Order` instance but I'm 
> not so sure it really is.
>
> The problem with the dirty plugin comes up when the non-order fields are 
> assigned something:
>
>
> one_order[:name] = "Testing"
> # undefined method 'name' for <#Order>
>
>
> This happens because name isn't actually an Order column, so when it 
> tries to get the value, there's no method available to provide it.  The 
> error happens in the change_column_value method of the dirty plugin.
>
> It seems to me as though the dirty plugin should verify that it's checking 
> valid columns for that model.  Is it worth updating the dirty plugin to 
> check that assignments are actually made to model columns, or is this a 
> case of using a model instance in the wrong way? (to be honest, I think 
> it's a little of both).
>

This is kind of an odd case (modifying returned values not part of the 
model's columns). Assuming that I am guessing the error correctly, you are 
hitting this line:

   iv[column] = get_column_value(column)

get_column_value in this case is an alias to send by default, but you can 
override if you want it to support additional columns.  Maybe something 
like:

def get_column_value(column)
  if respond_to?(column)
    super
  else
    values.fetch(column)
  end
end

I think that may give you what you want.

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/152c1bda-7b41-4d58-911d-e66f71d05ba4%40googlegroups.com.

Reply via email to