down
drop_table :styles
drop_table :style_types
end
end
CHAPTER 7 ?– A RAILS CMS 119
The migration for styles is a bit more complex, because you first need to create a table for
style types, and populate it with the values CSS and XSLT. You could have saved this information
in a string field in the style table, but it??™s good database design to externalize this
information. That way, you don't have to change things in many different places should you
decide to add a new style type.
A style is just a load of data that has a name and a type.
As you can see in Listing 7-4, a Layout is a collection of styles in a specified sorting order.
You save this information in a separate table called stylings. You could have used a join table
for this purpose, but when adding an attribute such as sort, it makes sense to have the styling
connection as a fully featured model object. You??™ll see how this makes sense later on, when
looking at the model classes.
Listing 7-4. db/migrate/004_create_layouts.rb
class CreateLayouts < ActiveRecord::Migration
def self.up
create_table :layouts do |t|
t.column :name, :string
end
create_table :stylings do |t|
t.column :layout_id, :integer
t.column :style_id, :integer
t.column :sort, :integer
end
end
def self.down
drop_table :layouts
drop_table :stylings
end
end
Listing 7-5 defines the migration for articles. An article should have a content type, so you
create a table for that first, and add three values for now.
Pages:
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210