Argument against the slow removal of DSL features in Rails, like dynamic finders

Hello all,

I’m becoming concerned with the apparent loss of English-like DSL in Rails. When I first came to Rails as an experienced C++/C#/.NET engineer, I was thrilled to write code that felt more like English.

Specifically, the belongs_to and has_many methods introduced me to a mental model of coding that expanded my mind, and wielded the power of the Ruby language in a way that no other framework that I had ever seen.

Rails 3 brought a slow death to another area of wonderful DSL-ness: validations. Instead of this:

validates_presence_of :title

we’re now supposed to use the generic form:

validates :title, :presence => true

which to me is uglier and doesn’t read nearly as well. I understand the other argument: By using a generic #validates method, it brings apparent consistency to all of the other validation lines, and lets you combine options, etc. But I don’t like it because it sacrifices a higher cause, which is code intention and readability.

I still use the old validates_presence_of and will continue to until it’s forcibly removed.

Maybe most people like the new validates method, so I didn’t want to complain. But now I see that dynamic finders are going away too! As someone who spends most of my time helping newcomers to Rails, I think this is a bad decision.

There are few things nicer than learning that I can find my data with methods like:

Product.find_by_title(“iPad Mini”)

Product.find_all_by_rating(4)

My understanding is that in Rails 4, these go away! Replaced by a more generic version, that takes a hash for everything instead.

We are slowly replacing our English-like DSL with generic methods that mean nothing. Meaning has been moved into an ugly hash instead.

What next? Would you put up with:

class Category < ActiveRecord::Base

associated_to :product, :ordinality => :many

end

I hope not!

So if maybe it’s too late to save the validation methods, but I hope it’s not too late to save dynamic finders.

Who’s with me? :slight_smile:

Thanks,

Jeff

Jeff, I agree that the old-style validation methods are better when you do not need to combine options. I’m +1 for keeping them around.

But the new dynamic finders are just as good as before:

Product.find_by title: “iPad Mini”

is just as well as:

Product.find_by_title “iPad Mini”.

Jeff, I agree that the old-style validation methods are better when you do not need to combine options. I’m +1 for keeping them around.

But the new dynamic finders are just as good as before:

Product.find_by title: “iPad Mini”

is just as well as:

Product.find_by_title “iPad Mini”.

Ah! I must have misread a description, I thought it was going to be #find with a hash.

Thanks for setting me straight,

Jeff

This is one of the big ticket items that drew me to Ruby (and to rails) in the first place. The expressive capabilities within your code using pre-established English-like DSLs. The loss of this aspect over time leaves me feeling rather sad. It feels as if Rails (and to some degree, Ruby itself) is moving towards a mind frame of terseness over expression. The ability to almost talk out the flow and type the exact things you just said and have it as workable code… mmmmm. Delicious.

I really hope that Rails does not move towards the idea that terseness is somehow better, that natural speech should not equal workable code, or drops the expressiveness currently inherent in the DSL.

My main issue with “validates” is that it’s easy to mix up with “validate” (validate (ActiveModel::Validations::ClassMethods) - APIdock).

If you accidentally do “validate :title, presence: true”, it will effectively do nothing, and won’t explode - it will call the “title” method and expect that method to add validation errors, and “presence: true” just becomes an ignored option.

Tests will catch this, of course, but it’s still an easy mistake to make.

I’m not sure what would be a good solution, though. Maybe complaining about unknown options?

My main issue with “validates” is that it’s easy to mix up with “validate” (http://apidock.com/rails/v3.2.8/ActiveModel/Validations/ClassMethods/validate).

This is my biggest issue as well. Every time I start writing validations, I have to refer to old code to see which method is correct.

I always do that too :frowning:

Same here… complaining about unknown options would help, but I’d also maybe suggest renaming “validate” to something like “validate_with”, which I think would be even more readable…