Its been a long time since I’ve not written a post. Now, I realize that it’s time to share some useful snippet with all of you.
Recently, I got a chance to get my hands cleaned with Rails 4. So much is happening around Rails community and with the rapid development of Rails 4, I was finding it bit difficult to keep my momentum going.
Well, leave all this apart. Now, by putting some extra efforts my system is ready with:
Ruby 2.1 & Rails 4.1
In order to start with my first Rails 4 Application, I took HABTM Association (weird, isn’t it?). It came randomly into my mind. However, I will dig into other associations as well with time, but to start with let’s take HABTM association with example.
I assume most of you are aware what changes Rails 4 has introduced. If you’re new to Rails 4 or would like to re-visit the changes, here’s the most useful link I’ve found. Just take a look.
Once you’re sure about changeset in Rails4, let’s draw an example:
Traditional Database (MySQL/Postgres)
A Join Table
Defining the Relationships & creating tables:
For rest of the code-flow (controllers, views) you can download the zip of the application from my GitHub page, run it on your local and see Rails4 HABTM in action.
Few days back a friend of mine came to me and said, “I am a RUBY Programmer and I am stuck in a requirement, wherein I have to read an excel-sheet (that contains multiple records and each record can have multiple entries in it.)”
Think of an example like given in the link: timesheet. It says we have 2 columns: EmployeeID and WorkingHours. Each EmployeeID can have multiple working hours.
Now, I would like to write a small Ruby program that iterate over each record in the excel, combine EmployeeID’s that has multiple entries and add working hours.
In this new tutorial, I am going to show you what most of developers/programmers already know, but not able to keep track of such things. I call it as ‘Ruby cheat-sheet‘, you can call it by the name you like.
This will also help those people who have just started learning ‘Ruby’.
I will keep on updating this article frequently.
Also need your valuable response, if you feel there is a better/easy concept available.
If I am not sure of something or don’t know the concept, I am going to highlight that with ‘Red‘ color. Later, you can put your opinion in the comment section and I will update that with your name.
Symbols (e.b :name, :age etc.) as just a convenient way of referencing a name.
You don’t need to per-declare a symbol.
A ‘Class‘ is a combination of state (for example, the name of the animal) and method that use that state (perhaps the method how animal eats).
The standard ruby constructor is ‘new‘.
Every object in Ruby has a unique ‘Object identifier‘ (called as Object ID).
Class have instance methods. These instance methods have access to the object’s instance variables.
Methods are defined with the keyword ‘def‘ followed by method name.
The most common way to create String objects in Ruby is ‘string literals‘. (E.g. “I am a string”, ‘I am another string’)
The logical difference between ‘Single quotes‘ and “Double quotes” in Ruby is the amount of processing Ruby does. In double quote case, Ruby first looks for substitution and replaces them with some binary value. (E.g. puts “Good Night, \nAuthor”)
Global variables: starts with ($) sign: E.g. $name
Instance variable: starts with (@) sign: E.g. @name
Class variables: starts with (@@) sign: E.g. @@name
Class name, module name and Constants: Starts with the uppercase letter: E.g class MyClass, module Login
Array stores collection of object with a key to access them.
An array can have objects of different types.
Hash contains key, value pair.
Ruby treats ‘nil’ as false in conditions.
A regular expression is a way of specifying a pattern of characters to be matched in a string.
The match operator (=~) can be used to match a regular expression against a string.
Yield (yield) is a Ruby method
In Ruby, nil is also an Object
When you run Ruby programs you can actually pass arguments. This concept is called command-line-arguments. For e.g. ruby my_example.rb firstname lastname city.
The initialize method in Ruby lets us set the state of each object. When we call ClassName.new to create a new object, Ruby allocates some memory to hold an UN-initialized object.
When we pass an object to putsbased on the class, it writes the name of the object’s class, followed by a colon and a unique identifier.
Ruby gives flexibilityto its programmers. You can over-write the default methods. (E.g. def to_s)
attr_reader is the modified way of accessor methods.
attr_accessor provides you to read and write methods. (E.g. attr_accessor :age)
‘require‘, ‘require_relative‘ & ‘load‘
Access Controls – ‘Public‘, ‘Private‘ & Protected
In ‘Ruby’ a variable is not an Object. It is simply reference to an Object.
‘initialize‘ is a private method in Ruby.
Block, Procs and Lambda are ways of grouping code we want to run. These are examples of closure in Ruby.
Both Proc and Lambda are Proc Objects.
Lambda check the number of arguments and throws argument error, whereas Proc does not.
In “Inheritance“, if class B inherits the property of class A, then all the methods of class A becomes the member of class B.
Assuming child = Child.new; then child.superclass => Parent
parent.superclass => Object
Object.superclass => BasicObject
BasicObject.superclass => nil
Modules: is a way of grouping together Methods, Classes and Constants.
Provide namespaces and prevent name clashes.
Supports the mix-in functionality.
A Module is not a class.
A Module cannot have instances.
We can always include a module in a class definition – After doing that all module instance methods are suddenly becomes available as methods in the class. They get mix-in.
mix-in modules don’t use instance variable directly. They use accessors to retrieve data.
self.method_name: Gives you access to the current object.
As I’ve seen lot many examples, articles over Single Table Inheritance, I decided to write my own. Thanks to the open source contributors. Their Articles/Examples helped me a lot writing this post.
As you might know about single table inheritance, let me quickly set the definition for it. Single-Table-Inheritance (STI) allows you to write/create sub-classes of a specific model. You can use a single table, add specific objects and extend the functionality/behavior of base model.
Current example explained:
My Current example is based on Mobile system. We have a single model called ‘Mobile’ and other models such as ‘Samsung’, ‘Apple’, ‘Nokia’ etc., that inherits the property of Mobile model. (Refer to the image for more help)
Development Platform used:
Ruby - 1.9.3
Rails - 3.2.13
DB - MySQL
IDE - Netbeans 6.9.1
The application is currently hosted on GitHub and can be cloned using the below command: git clone email@example.com:puneetpandey/rails3_singleTableInheritance.git