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.
To do so I need the name of the model. As my test object, I cleverly naming it
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
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 …
If you’re starting out a certificate for Microsoft Certified Professional (MCP) for just about anything OS related + 1 or 2 years of experience answering phones will get you in the door for basic helpdesk support at one of the small or midsized companies, no college required…
- Atomic $14-16 – Link
- Erickson $14-16 – Link
- Driving version of helpdesk – $19-22 – Link
- IT Retraining program for minneapolis – Link
Stretching it a bit is the big companies, they will want a year experience in something or equivalent college.
- Honeywell – IT Support Tech support liaison – Link
Sega Genesis “Lost Vikings” by Blizzard Games!
In my previous blog post I talked about reverse engineering the virtual machine used to implement objects in the Lost Vikings. The Lost Vikings was released in 1993 by Silicon and Synapse, now better know as Blizzard.
At the time I empirically tested some opcodes by manually patching the original data chunks with a hex editor. This works, but it fairly tedious, and doesn’t scale well to experimenting with larger programs, or programs that contain loops or jumps. In the first blog post I suggested that creating a simple language and compiler would be useful for further reverse engineering the virtual machine. So, I did.
Building a Compiler
To assist reverse engineering the virtual machine, and also allow for easy creation of new programs I decided to build a compiler. To keep things simple I opted for a single pass, recursive descent parser.
The compiler design is very much based…
View original post 2,161 more words
- Free code camp … 800 hrs of coding to prep you for volunteering time to get real world experience. Link
- Launchschool’s Capstone Mentoring – 10% of your first year employed – Link
- Viking Code School –
- Immersive: $12k flat or 20% of first year (paid in first 6 months) of working – Link
- ~$300 / month – forum, chat, weekly mentor checkins, live virtual office hours
- ~$600 / month – all of the above plus 1 on 1 mentor
- ~$1400 / month – career mentoring, custom paths, 3 meetings a week
interesting…apparently their “new” site is down already though 😦
The Cells gem has helped many developers to re-structure and re-think their view layer in Rails. It provides view models that embrace parts of your UI into self-contained widgets.
What was partials, filters, helpers and controller code is now moved into a separate class. View models are plain Ruby and use OOP features like inheritance while benefiting from encapsulation. The times of global view namespace and lack of interfaces in views are over.
class CommentCell < Cell::ViewModel def show render end private def author_link link_to model.author end end
Cells can render their own views which sit in a private directory.
In views, we try to gently enforce simplicity: When calling a method in the view, it is called on the cell instance. The view is always executed in cells context. There is no concept of “helpers” and data being copied between controller and view anymore.
View original post 1,308 more words