Sign in through GitHub

RailsCasts Pro episodes are now free!

Learn more or hide this

Nicholas Henry's Profile

GitHub User: nicholasjhenry


Comments by Nicholas Henry


When unit testing the Service Object I can easily stub the method find_by_username. However the where would required an ugly stub chain. If the API changes the where clause is more costly than the find_by_username.

Having said that, there maybe a day when find_by_username is deprecated like it's cousin, find_all_by_ in Rails 4 and I would regret that choice :-)

If working on a team and they wanted to move the method find_by_username to the User I would be be totally cool with that, perhaps using the ActiveRelation syntax to build the query.

I know some folks advocate even hide the all method within their model, for example:

class Post
  alias_method :all_posts, :all

Anyway, I hope that gives you some idea into my thought process behind that :-)


Thank you for highlighting the use of Service Objects, it's a technique that I have adopted and felt has improved the codebase for the projects I have worked on. There's probably one piece of code I would push back down into the model:

User.where(@omniauth.slice(:provider, :uid)).first_or_initialize

This query method exposes the ActiveRecord Query API and I would rather have that encapsulated in the ActiveRecord model. find_by_username is debatable, but I typically let those slip.