Tuesday, April 5, 2011

Codeigniter's insert_id()

I thought the insert_id() in codeigniter will insert an empty row and return the id. But I am wrong.

You should call insert() first and then call insert_id() to retrieved the id of newly inserted row.

like this:


$this->db->insert('table_name', $data);
$db_id = $this->db->insert_id();

Sunday, September 7, 2008

Rails Migration Data Type


Basic data types:


:integer
:float
:datetime
:date
:timestamp
:time
:text
:string
:binary
:boolean


Valid column options:


:limit
:null (:null => false implies NOT NULL)
:default (to specify default values)

Thursday, August 21, 2008

A new project started.



I started a new project on Aug. 20, 2008.

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.

Friday, August 1, 2008

The two purposes of Rails Routing


Rails Routing is defined in ./config/route.rb. Routings are actually a list of rules.

Routing has two purposes.


  • Recognize URL: What to do when an URL like
    http://localhost:3000/myrecipes/apples
    is coming.

  • Generate URL: What to create when Rails command like

    link_to :controller=>'recipes',
    :action=>'show',
    :ingredient=>'apple'

    is defined.




map.connect "myrecipes/:ingredient",
:controller=>"recipes",
:action=>"show"



The static text is myrecipes.

The wildcard receptor is :ingredient which you can get by
params[:ingredient]

Wednesday, July 30, 2008

Plugin for Login and Authentication


The RESTful login plugin from Rick Olson is a handful method to implement a user login mechanism.

Some links below for future reference.

RESTful Authentication

To install this plugin, use command

ruby script/plugin install 
http://svn.techno-weenie.net/projects/plugins/restful_authentication/


For single id sign on, use OpenID authentication

OpenID Authentication

Saturday, July 26, 2008

Ruby's block


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".


def twice
yield
yield
end




When you call this iterator, it just yield twice that means the caller's block will be invoked twice.


twice {puts "hello"}

output: hello hello.


Of course you can pass value from Iterator method to yield block.


def sequence(n, m, c)
i = 0
while(i < n) # Loop n times
#Invoke the block and pass a value to it
yield m*i + c
i += 1 # Increment i each time
end
end

The Iterator method is invoked by:
sequence(3, 5, 1) {|y| puts y }



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".


def sequence(n, m, c, &b)
i = 0
while(i < n) # Loop n times
#Invoke the block and pass a value to it
b.call(m*i + c)
i += 1 # Increment i each time
end
end

The Iterator method is invoked by:
sequence(3, 5, 1) {|y| puts y }
# Note that the block is still passed outside of the parentheses

Thursday, June 26, 2008

Render a collection


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

animal_controller.rb

Class AnimalController < ApplicationController
...
def show
@animals = Animal.find :all
end
...
end


Now, how shuold we render the collection?

Usually we use 'for each' against the collection to render the table:

show.html.erb

<table>
<tr><td>Specie</td>
<td>Age</td>
<td>Weight</td>
<td>Risk</td>
</tr>
<tr>
<% for each @animals do |animal| %>
<td><%= h(animal.specie) %></td>
<td><%= h(animal.age) %></td>
<td><%= h(animal.weight) %></td>
<td><%= h(animal.risk) %></td>
<% end %>
</tr>
</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:

show.html.erb

<table>
<tr><td>Specie</td>
<td>Age</td>
<td>Weight</td>
<td>Risk</td>
</tr>
<% render :partial => 'animal', :collection => @animals %>
</table>


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:

_animal.html.erb

<tr>
<td><%= h(animal.specie) %></td>
<td><%= h(animal.age) %></td>
<td><%= h(animal.weight) %></td>
<td><%= h(animal.risk) %></td>
</tr>


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).

_animal.html.erb

<tr>
<td><%= animal_counter %></td>
...
</tr>


Don't forget using h(...) function to sanitize your passed in variable.