The project is called resume generator which will automatically generate a resume based on your input data and export in various formats like pdf, txt, xml, etc.
The project is located at http://xianese.unfuddle.com
Database schema has been done. Here is the diagram.
Ruby's block always make a developer like me who is being with typed language for long time confused.
In Ruby, there are some methods called Iterators like times, each, map, upto and downto and some objects called Enumerable objects like array, hash and range which contain many Iterator methods. The common feature of these methods is that they are all interacted with some custom codes.
A group of these codes for a method is called "a block".
For example:
[1,2,3].map {|x| x*x}
[1,2,3] is an Enumerable object(an Array)
"map" is an Iterator(method) which will transverse all elements in the Array
{|x| x*x} is a block provide by user for callback from "map" method
a "yield" method is provided in "map" to call block code
So, block is a group of custom codes waiting for Iterator method to call. Each time when a "yield" is met, block is called.
Here is a very simple example about an Iterator method call "twice".
When you call this iterator, it just yield twice that means the caller's block will be invoked twice.
Of course you can pass value from Iterator method to yield block.
The passing block parameter ({|y| puts y}) can be explicitly declared in the Iterator. Once it is declared, "call" method is used to invoke the block instead of "yield".
Very often we want to list a collection of data in front HTML page.
Let's say we have an animal collection. Each record of this collection contains animal Specie, Age, Weight and Risk. As you can imagine, the list is retrieved by
Now, how shuold we render the collection?
Usually we use 'for each' against the collection to render the table:
But there is a better solution with a bonus feature, a counter of records. Once you are used to this solution, you most likely will not turn back.
The solution is to use partial with :collection function and key is NAMING CONVENTION.
The collection partial is passed in by this way:
Partial's name 'animal' will be passed into partial as a varible. It represents one record in the collection. So, 'animal' can be used to reference one record in the collection.
Here is the partial:
You can also add a new column to count the rows. There is a 0 based counter avaiable in partial. By naming convention, the counter variable is called animal_counter (partial-name_counter).
Don't forget using h(...) function to sanitize your passed in variable.
As you know you can access a Rails project via browser by calling
http://localhost:3000/.../...
But when I try to access this project from other computers by calling
http://[computer_name]:3000/.../...
or
http://[ip_address]:3000/.../...
, IE or FireFox just can't reach the web page.
This problem bothered me for a while. I read some articles about how WEBrick works but did find how to make visible to other computers.
Today I have to ask helps from Rails community and finally found the solution. There is nothing to with how to config the WEBrick. It is simple as this:
Ruby script/server -b [ip_address] -p [port]
Now I can access the pages from other computers. Pretty happy.
Yesterday night, my mom was terribly scared by a raccoon. She said it made a funny face when she was watching TV.
This brought a question to me. Is raccoon offensive? If not, why was my mom scared?
So, if I create an label and attach it to anything (I mean anything, not just animals) which are offensive, my mom should NOT be scared.
Since the label is going to be attached to ANYTHING, it brings me an association dilemma. If a Offend label is attached to Raccoon class, an association from Raccoon to Offend is established. If another Offend label is attached to Dog class, another association to Offend is also established. So, Offend must hold both 'foreign keys' to Man and Dog. But each record of Offend class always has one foreign key with Null value. For example, if Offend record 1 points to Raccoon, the foreign key to Dog must be Null. Vise versa.
In Ruby On Rails, 'polymorphic has_many association' can solve the problem. 'polymorphic has_many association' hooks a particular class to any kind and number of sources classes. These source classes may not be related.
First, let's define an INTERFACE called offensive (you can use any name) and declare it is polymorphic.
Second, let's create a Offend class which will be attached as a label to other classes. Then, we make Offend class belong to the INTERFACE.
Third, create two columns in table offends: offensive_id and offensive_type
In Rails 2.0+, a new macro style method 'references' is introduced to automatically create the INTERFACE's id and type.
But I found only offensive_id was generated, not offensive_type. The INTERFACES's type field is used to hold the classes' type of which the Offend class will be attached to, such as Raccoon or Dog. So more research on 'references' method is needed in the future.
Now we are ready to attach this class to any source classes.
First, simply declare has_many offends via offensive INTERFACE.
Second, create a Offend object and update its :offensive attribute by passing the source objects (like instance of Dog or Raccoon) in.
Now we can see, by attaching the class Offend, Raccoon is more dangerous then Dog.
A problem I found is how Model class inheritance reflects in table relationship. When I saw the Single-Table Inheritance (STI) introduced by Martin Fowler a couple of years ago, I thought it was really stupid.
The whole idea of STI is simple: include all super/sub classes' attributes in one big fat single table and use an extra column (may be called 'type') to distinguish different type of classes.
But the default rails way is STI. :(
Now I have a Model class User and want to have another Model class called VIPUser which is a subclass of User. VIPUser has a new attribute called vip_number which User doesn't have.
OK, let's do the magic.
First, create a VIPUser Model
Second, edit the migration script for VIPUser
Notice that two new columns are created in the users table (which is for Model class User). One is the attribute of VIPUser, 'vip_number' and another is 'type' which is used to distinguish the type of classes.
If you've already had a column called 'type' or you don't like the name, you can use any other names and later declare it in VIPUser Model class.
Third, make VIPUser a subclass of User
That is it!
Now if you create a new VIPUser, you will see Rails automatically populate a string 'VIPUser' in type field.
In many cases, bank managers ask you to keep an account information even it is closed or removed. Usually we keep the record in database but mark it by a flag column like 'delete_at' or 'status'.
To provent the false deletion, a method is registered to callback before_destroy.
Notice that return false is very important. It halts the execution of the further deletion action.
Also notice that delete and delete_all ActiveRecord model methods will bypass the before_destroy callback methods.
Kitty students start to register courses. A student can have many courses and a course can contain many students. Students and courses are many to many relationship via registration.
Now, the problem is, how to make sure each kitty is not registered more once for a particular course.
To do this, use validates_uniqueness_of
Validate the uniqueness of a kitty student id (kitty_id) against a course_id:
Remember! when validating the uniqueness via has_many :through, use specific attribute name instead of model name.
Yes, give me :five! Enough :first and :all, I want to find :five.
In order to do this, I checked the source code of find :first and find :all in base.rb
OK, the next step is easy, override this method by defining a class method of a Model class, let's say, Blog class and add :five argument in.
Use class << self to open the Blog class object and add the find method in to override the original one. You have to implement :first and :all again. Otherwise the "Blog.find :all" will not work.
Now, you can use find :five to retrive the fifth elements:
If a Model class has a "has_many" relationship available, it can specify a "has_one" relationship pointing to one record for future convenient reference.
:conditions and other SQL options can be used to narrow the search.
Blog has many Post
Later, you can reference the first record easily by:
Yes! this is the one I love most, give her some special treat.
When reading the Active Record, I was wondering if I can create a rake to automatically populate some data into database.
After some research, I have the following code samples.
Suppose I have three tables: blogs, posts and comments.
And I have three prepared yaml files: blogs.yml, posts.yml and comments.yml. They are saved at /#{RAILS_ROOT}/lib/tasks/sample_data/
blogs.yml looks like:
Then the rake file "sample_data.rake" is saved under /#{RAILS_ROOT}/lib/tasks/.
I use TABLES array to hold the table names.
From table names, I use Inflector's classify function to convert the table names to Model Class names and then use const_get function to get the ActiveRecord Class instance. You can't directly use a string name to refer to the Class instance. So this step is necessary.
Next step is easy, ust YAML.load_file to load each yml files:
Notice that the data is actually a hash like "B1000" => <...>. The <...> is another hash with real data: blog_name => "Xianese's blog", etc. So you have to tranverse the hash then use model_class to create a new instance of the Model object and save it:
That is it! It is pretty simple.
Removing sample data is even simpler. I use ruby's eval method to pass in a class string name and delete all records for each Model.