Thanks Jeremy that sounds like a perfect solution. I agree, it's an odd case, and I really don't think it's a good idea to use a model instance like this, because it's not really a model instance any more when you've added properties that don't exist in the database.
That said, the example I gave was overly simplified. In the case I ran into, existing code was iterating through a collection of rows, saving a `initial_value` to aid in later processing. The problem didn't reveal itself until I started using the dirty plugin. I will definitely try out your suggested solution. As always, thank you for the amazing work you've done on Sequel. Jason On Tuesday, July 30, 2019 at 10:04:43 PM UTC, Jeremy Evans wrote: > 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/d8c38c08-97d7-49c9-838a-d7af3e1c323a%40googlegroups.com.
