SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 200 | Next

Ola Bini

"Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java"


Wow. That was many database definitions quickly. Now, let??™s migrate this and take a look
at the corresponding models:
jruby -S rake db:migrate
The Model
After creating the database, it??™s time to get the model in shape. You aren??™t adding validations or
testing at the moment, just so you can get something working fast. You??™ll go through the mod-
CHAPTER 7 ?–  A RAILS CMS 121
els one at a time in the same order as you created them in the database. There??™s almost nothing
new here, so I??™ll present the model files straight up. Let??™s begin with the User model:
class User < ActiveRecord::Base
has_many :articles
end
Next up, create the Path model:
class Path < ActiveRecord::Base
belongs_to :layout
has_many :articles
end
Now, styles are a little bit more complicated. One reason for this is that you need to create
a new file for the model called app/models/style_type.rb:
class StyleType < ActiveRecord::Base
has_many :styles
end
Next, create the Style model:
class Style < ActiveRecord::Base
belongs_to :style_type
has_many :stylings, :order => 'stylings.sort'
has_many :layouts, :through => :stylings, :order => 'stylings.sort'
end
As you can see here, you have a few new things. First of all, you use the Style model, and
order it. You might remember that you created a table for stylings in the migration for layouts.
The model association between Style and Layout is mediated through stylings. This
works mostly the same way as a join table, but it lets us add attributes to the association in a
way that??™s much easier than has_and_belongs_to_many.


Pages:
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212