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

Ability to use Active Record class methods #19

Open
arturopie opened this issue Feb 11, 2019 · 4 comments
Open

Ability to use Active Record class methods #19

arturopie opened this issue Feb 11, 2019 · 4 comments

Comments

@arturopie
Copy link
Contributor

arturopie commented Feb 11, 2019

We like how, by using the Vorpal Mapper, we can use AR methods like #where inside a repo. We'd like the ability to use other methods like #find or #find_by as well, but these methods return an AR object instead of an ActiveRecord::Relation object, and that AR object does not have the useful load_one method.

Here is an example

This method:

    def find(purchase_order_id)
      record = query.find(purchase_order_id)
      record.load_one
    end

Crashes with the following error:

NoMethodError: undefined method `load_one' for #<#<Class:0x00007f9494e63b80>:0x00007f9494d05540>

We want to use find because we want to raise a NoError exception if the record does not exist.

@sskirby
Copy link
Member

sskirby commented Feb 12, 2019

Did you want to raise an ActiveRecord::RecordNotFound exception when nothing is found given an id?

Also, the dynamic finder methods appear to be mildly deprecated. Why use them instead of the AREL style?

@arturopie
Copy link
Contributor Author

@sskirby yes, we want the NotFound exception so our controllers return 404s. Right now we are reimplementing that behaviour in our repos, but having this in Vorpal/AR would be an enhancement and will lower the bar to use Vorpal.

We don't want to use dynamic finders (e.g. #find_by_name), but just #find_by(name: n) which is not deprecated.

@sskirby
Copy link
Member

sskirby commented Feb 13, 2019

I'm sure we can come up with something to make the your ActiveRecord::RecordNotFound use-case easier!

I'm curious why you would like to use find_by(name: n) instead of something like where(name: n)?

@arturopie
Copy link
Contributor Author

.find_by(name: n) is equivalent to .where(name: n).first. So I guess it's just more convenient (one less call). It also feels more like Rails, so it's easier for people that are used to use Rails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants