You can still work with these associations
in the regular has_many ways.
The next model you should look at is the one for Layout:
class Layout < ActiveRecord::Base
has_many :stylings, :order => 'stylings.sort'
has_many :styles, :through => :stylings, :order => 'stylings.sort'
has_many :articles
has_many :paths
end
Here you see the inverse of the relationship, sorted the same way. You need to create the
styling model by hand, so add a new file called app/models/styling.rb:
class Styling < ActiveRecord::Base
belongs_to :style
belongs_to :layout
end
CHAPTER 7 ?– A RAILS CMS 122
Finally, you need a new file called app/models/content_type.rb so you can add the Article
model, too. You should define ContentType like this:
class ContentType < ActiveRecord::Base
has_many :articles
end
Add Article like this:
class Article < ActiveRecord::Base
belongs_to :user
belongs_to :content_type
belongs_to :path
belongs_to :layout
def matcher
Regexp.new('^'+self.name+'$')
end
end
Notice that you use the name attribute to create a new regular expression that??™s anchored
at both ends. That means the matching should only be done on a complete string. You??™ll see
later how you use this part of the model. This is all there is to our model at the moment. It will
be more complicated later, though.
Some Layout
Well, you have the model in place??”at least partly. It??™s time to add some looks, and then to start
work on the administrative user interface.
Pages:
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213