Finding the class based on an object instance of the object in Rails with Constantize

Trying to write a rails method to count the rows in a table.

The twist:  I want to do it in such a way that I can call the method from any object and it will automatically determine the name of the table to count the rows from.

Basic outline of the flow … I want to use Rails Way to identify the table & count the resulting table name’s rows.

Model.count

To do so I need the name of the model.  As my test object, I cleverly naming it anObject

2.3.1 :040 > anObject = Player.first
 Player Load (2.4ms) SELECT "players".* FROM "players" ORDER BY "players"."id" ASC LIMIT $1 [["LIMIT", 1]]
 => #<Player id: 1, screenname: "Serena Mitchell I", motto: "Wyaaaaaa.", country_id: nil, created_at: "2017-09-01 12:52:27", updated_at: "2017-09-01 12:52:27">

Rails has several definitions which hold that information in various states.  I chose to use .class …

2.3.1 :043 > anObject.class
 => Player(id: integer, screenname: string, motto: string, country_id: integer, created_at: datetime, updated_at: datetime)

When we look at this, it’s more information than we need.    The Rails Way is to call the .name method out of the class method …

2.3.1 :046 > anObject.class.name
 => "Player"

As a bonus .name gives us access to .constantize which is the goal.

2.3.1 :048 > anObject.class.name.constantize
 => Player(id: integer, screenname: string, motto: string, country_id: integer, created_at: datetime, updated_at: datetime)

Now we can put .constantize to search all the Rails app’s constants …

2.3.1 :050 > anObject.class.name.constantize
 => Player(id: integer, screenname: string, motto: string, country_id: integer, created_at: datetime, updated_at: datetime)

… which when proceeded by the .class.name will give the activerecord model as an object which we can then .count against …

2.3.1 :052 > anObject.class.name.constantize.count
 (0.8ms) SELECT COUNT(*) FROM "players"
 => 46

At this point .class.name.constantize should net us what we need, so I wrap it in a method for easy reuse …

#  gets name of constant to get a count of model
def row_count(ar_object)
  ar_object.class.name.constantize.count
end

This of course isn’t perfect, if ever you had an conflict in the naming scheme, scope issues or an orphaned object from the inheritance tree, the .constantize search up the object inheritance tree would fail even though your object exists.

Note there is another way, that if you are only doing ActiveRecord objects works too … Link.  My example is slightly more generic example as you can use it to find other types of objectives – though you can’t use the count method with most of them.

The reason you would use it, is that it’s more self documenting, thus more in line with the Rails Way ….

# .model_name.name is basically the same as .class.name for this purpose
ar_object.model_name.name.constantize.count

My studies from other authors which I found helpful, in no particular order …

  • Failure points & discussions for constantize – Link
  • Implementing in a custom module from Stackoverflow – Link
  • Implementing the constantize lookup in depth & use – Link
  • Safelty constantize in forms, aka don’t – Link
  • Metaprogramming & testing with constantize – Link
Advertisements

Rails organization / patterns

Been summarizing some of my studies – note none of this is confirmed by an official Ruby on Rails historian …

I started out looking for where code was supposed to be placed.  Why?  Because I read about DRY.  I really liked the idea of not wasting time redoing code – it’s one of the reason I got into Rails, which I will call RoR from here forward.

So people divide the RoR apps up according to MVC philosophy, which the short version is the model of the database logic, the view you see & is’ logic, and the controller with its logic to tie the two together.

In this MVC design, when I entered RoR’s world most posters were talking about Fat Model / Skinny Controller.

I started reading a lot about this stuff … Justin Weiss has a great breakdown summary of different patterns & where they get placed in the RoR app – Link.  I also like the description of the process on Stackoverflow from ‘Jason‘ & Mike Woodhouse – Link.

 

From what I can gather from posts there was a development of programming as people moved along.  Notice I’m not numbering these…there’s a reason…

  • model / controller / views
  • helpers (with special visibility to only their views)
  • custom classes anywhere
  • app/lib as “the spot” which at one point in RoR history auto loaded (not anymore)
  • modules / mix-ins to include the classes/methods
  • gems – which
  • plugins – which were discontinued in Rails 4, but the command used to make engines now – Hawkins had a good break down if somewhat biased – Link
  • concerns – which even had a special activesupport::concerns for including classes added
  • rails engines which improve on the modules>gems by encapsulating the database too … often these are wrapped in gems when created with mountable option – this might be part of the micro-services movement…

Trouble shooting reminders for me…

Trouble shooting

  • Did something actually break?
    • Is it perspective/perception thing instead of a solid problem?
    • Is it that you don’t have data/values or they aren’t being connected?
    • What does tail’ing the test/development log files show?
  • State changes
    • Does a reload / restart of server / using other device fix it
  • Where is the break at based on what is affected?
    • Can you output flags or sentences to help identify where firing?
  • Scope
    • How many things  affected?
    • How deeply or totally were things affected?
  • What was your last change?
    • What was your last commit to that branch?
  • Similarities / Shared in the systems / characteristics of  the affected systems / components
  • Can you comment stuff out until it works?
  • Does changing test coverage or stubbing work?
  • Does using the code in isolation work?

The difference between a Jr & Senior programmer at Gitlabs

https://about.gitlab.com/jobs/build-engineer/

 

Junior Build Engineer

Junior Build Engineers are developers who meet the following criteria:

  1. Technical skills
    • Is able to write code in required languages but needs guidance in writing modular and maintainable code
    • Has less experience to no experience with containers
    • Proposes default configuration to reduce the need for configuration by customers
  2. Code quality
    • Leaves code in substantially better shape than before
    • Is able to write clear documentation
  3. Communication
    • Needs help with time management
    • Is able to follow technical conversations within the team
  4. Performance & Scalability
    • Needs help writing production-ready code
    • Has little to no experience writing large scale apps

Senior Build Engineer

Senior Build Engineers are experienced developers who meet the following criteria:

  1. Technical Skills
    • Are able to write modular, well-tested, and maintainable code
    • Know the domain really well and radiate that knowledge
    • Contribute to one or more complementary projects
  2. Leadership
    • Begins to show architectural perspective
    • Proposing new ideas, performing feasibility analyses and scoping the work
  3. Code quality
    • Leaves code in substantially better shape than before
    • Fixes bugs/regressions quickly
    • Monitors overall code quality
    • Reacts efficiently to build failures
    • Creates test plans
  4. Communication
    • Provides thorough and timely code feedback for peers
    • Able to communicate clearly on technical topics
    • Keeps issues up-to-date with progress
    • Helps guide other merge requests to completion
    • Is able to help with recruiting
  5. Performance & Scalability
    • Excellent at understanding the cost of a change
    • Excellent at writing production-ready code with little assistance
    • Able to write complex code that can scale with a significant number of users