I have a simple model:
class Project < ActiveRecord::Base
belongs_to :user
class User < ActiveRecord::Base
has_many :projects
Project has a few attributes like :date, :name, and for those attributes I have attribute query methods available to me:
project.date?
project.name?
But I don’t have:
project.user?
Instead I have:
project.user_id?
A glance at the code shows this is because the actual “attribute” is the user_id, and that’s all that the attribute query methods are intended for? It seems like a logical extension of the idea to have query methods for the relations as well.
What’s the common practice? Seems we have a few rough equivalents to choose from:
project.user.present?
project.user.blank?
project.user.nil?
Or just use the {relation}_id? method and stop whining?
Why not just define a method on project yourself to meet the API that you are expecting?
I think the best practice in Ruby, and frankly in all programming is to clearly specify your interface, and if you think that user? is the best way to do that, then I say go for it.