Hello guys, hope you are doing well.
Recently I got some code to review and saw a lot of code inside my project, and found something interesting to discuss about.
I have some codes that needs to check if an record model(e.g.: Book) belongs_to another record model class(e.g: User or a Guest), here I have to do see if it has an user_id or an guest_id, and then I check it class like:
if Book.column_names.include('user_id')
...
end
or alternatively I could do:
Book.reflect_on_association(:user)
But here is my proposal, create an method called: belongs_to?(association) , could potentially create has_many?(association), and has_one?(association) to check associations inside a record model class, the usage would be something like that. e.g:
Book.belongs_to?(:user)
Book.has_one?(:user)
Book.has_many(:guest)
What do you guys think about this? Curious to hear about