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:
Bingo, I got a :five!
Saturday, May 31, 2008
Friday, May 30, 2008
The one I love most
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.
: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.
To be singular or to be plural
ActiveRecord's relationship is sensitive to the plurality of the database table name.
Let's say, blogs contains many posts, then blogs should be on singular side and posts on plural side.
For has_one and belongs_to relationship, it should be singular. Suppose each user has only one avatar:
If you don't follow the convention, it won't work.
Let's say, blogs contains many posts, then blogs should be on singular side and posts on plural side.
For has_one and belongs_to relationship, it should be singular. Suppose each user has only one avatar:
If you don't follow the convention, it won't work.
Wednesday, May 28, 2008
Use YAML to load sample data into database
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.
Here is the final code:
I hope this helps.
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.
Here is the final code:
I hope this helps.
Subscribe to:
Posts (Atom)