The Order
model in the Spree framework "has many" line items, described like so:
has_many :line_items, -> { order('created_at ASC') }, dependent: :destroy, inverse_of: :order
I've added a model called SelfFulfillmentUnit
which "belongs to" a line item (a line item can have many SelfFulfillmentUnit
objects), simply described as such:
has_many :self_fulfillment_units
I would like to be able to access all of the Self-fulfillment Units associated with this order through the line items, so I have an order decorator which creates this relationship described like so:
has_many :self_fulfillment_units, through: :line_items
The Problem:
Unfortunately, when I do this:
order.self_fulfillment_units
I receive this error:
ambiguous column name: created_at: SELECT spree_self_fulfillment_units.id FROM "spree_self_fulfillment_units" INNER JOIN "spree_line_items" ON "spree_self_fulfillment_units"."line_item_id" = "spree_line_items"."id" WHERE "spree_line_items"."order_id" = 13 ORDER BY created_at ASC
I'm fairly certain this is caused by the lambda condition in the has_many :line_items
of the Order
model.
Assuming that is the case, I'm not certain how to prevent the condition block from executing. As per this answer, I tried adding the unscoped
method to the order select call (Spree::Order.unscoped.by_number....
), however this did not seem to have any effect (the ORDER BY created_at ASC
portion remained).
The only solution I've been able to find is to add this to the order decorator:
has_many :line_items, dependent: :destroy, inverse_of: :order
effectively overriding the line_items
relationship line from the base order model. This, however, is obviously not an ideal solution, since every call to the line_items
relation is now affected.
Alternatively, I could create a custom line items has_many
relationship with a different name, but without the condition block.
Question: Other than that solution though, I'm now sure how else to solve this. Is there a way to somehow suppress the condition block of a relationship statement?