I just started building a new Rails application in version 2.3.4. One feature that I thought was particularly handy is the data seeding that is now built into Ruby on Rails.
Before this feature, you would have to do one of two things. You could seed your data in your migrations. The problem with this approach is that it clutters up your migrations, and can make for more brittle migrations. It also may or may not propagate to your test database when you run your tests, meaning that if you’re counting on it, it may not bee there.
Your second option was to create fixtures and a rake task to import the fixture data into your Rails application. The problem with this is the need to create multiple related objects across multiple files to make all of your data match up, which can create maintenance problems.
So, without further ado, here is the solution now included in Rails. You simple create a file at db/seeds.rb and place ActiveRecord create calls in the file. Here’s an example seeds.rb.
Read More
2.3.4, activerecord, create, data, data seeding, Database, fixtures, migration, rake
One of the most powerful aspects of Ruby on Rails are the associations we can create between two classes. It is immensely convenient to be able to call person.posts rather than doing a SQL statement to find all of the posts with a person_id of X.
Sometimes, we have instances where the associations could be with multiple classes. For example, if we have a Page class that can be associated with an HTMLAdvertisement or an ImageAdvertisement. In that case, we really want to be able to call @page.advertisement to get the advertisement. This is where polymorphic associations come in.
Polymorphic associations allow us to associate a single attribute of the class to any number of specified classes. Here are the Page, HtmlAdvertisement, and ImageAdvertisement models:
Read More
activerecord, Database, migration, mixin modules, polymorphic associations, restful routes
A lot of new Ruby developers I’ve worked with have seen the symbol notation—starting with a :— and have been confused by what a symbol actually is. There is a lot of information out there that is confusing as well. Here’s a brief rundown of what symbols are and how they are used.
Read More
activerecord, Developers, development, Ruby, symbol
I was browsing the ActiveRecord documentation and I came across notation that looks like this:
When I looked more closely, I realized that it replaced all of the places where I had something like this:
In other words, if the user’s name is an empty string (“”) or nil, I can call User.name? and it’ll return false. Here’s the documentation I was browsing. http://api.rubyonrails.org/classes/ActiveRecord/Base.html
activerecord, Rails, Ruby on Rails
Rails Developers do it with Models
I saw a t-shirt at Mountain West RubyConf this year that said “Rails Developers do it with Models.” Of course, they were talking about the classes we use to access the database. In fact, in Ruby on Rails, when you think of your data, you usually think of Models, not the database. The database is more a mechanism for remembering the data when you’re not using it.
Let’s look at one of the files that were generated in Part I. This file is where the model for our blog’s posts are defined.
Read More
activerecord, Database, migration, MySQL, Rails, Ruby on Rails, tutorial