ash icon indicating copy to clipboard operation
ash copied to clipboard

Add offet / limit to has_many and has_one relationships

Open enoonan opened this issue 11 months ago • 2 comments

Is your feature request related to a problem? Please describe.

I was hoping to create a relationship that would grab the second newest record in a list without needing to create a Manual Relationship. My attempts thus far have failed because the has_one relationship DSL lacks an offset function.

Describe the solution you'd like

I'd like it if offset was included in the has_many and has_one relationships, and if limit was included in the has_many relationship.

Describe alternatives you've considered I tried using read_action, pointed at a read action that used a query preparation to add sort, limit, and offset. Alas it seems to have gotten stripped out because it's just not intended to work that way. In this case, since it was a has_one relationship and the final query didn't include the limit clause, Ash returned an error saying it received multiple results when it expected only one.

My next approach will be to use a Manual Relationship, as suggested by Zach! But I'd love to have it in the DSL because as I understand it that means I can compose it more easily with other data layer operations, plus it just looks nice and is easier to do.

Express the feature either with a change to resource syntax, or with a change to the resource interface

    has_one :previous_log, Log do
      sort inserted_at: :desc
      offset 1
    end

    has_many :ten_previous_logs, Log do
      sort inserted_at: :desc
      offset 1
      limit 10
    end

enoonan avatar Jan 28 '25 23:01 enoonan

We've added limit, perhaps someone would be intereseted in contributing offset based off of that.

zachdaniel avatar Jun 03 '25 14:06 zachdaniel

I'd be up for that now that the pattern is established :)

enoonan avatar Jun 03 '25 15:06 enoonan