nulogy / vorpal

Separate your domain model from your persistence mechanism.
MIT License
13 stars 5 forks source link

Ability to use Active Record class methods #19

Open arturopie opened 5 years ago

arturopie commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

.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.