Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add offet / limit to has_many and has_one relationships #1740

Open
enoonan opened this issue Jan 28, 2025 · 0 comments
Open

Add offet / limit to has_many and has_one relationships #1740

enoonan opened this issue Jan 28, 2025 · 0 comments
Labels
enhancement New feature or request needs review

Comments

@enoonan
Copy link

enoonan commented Jan 28, 2025

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 enoonan added enhancement New feature or request needs review labels Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs review
Projects
None yet
Development

No branches or pull requests

1 participant