Prev | Current Page 263 | Next

David Berube

"Practical Reporting with Ruby and Rails"

Suppose you want to insert some data into a table named webhosts, which
has two columns: domain and description. You could use the following Active Record
code:
Webhost.new(:domain=>'www.somecompany.example',
:description=>'Some Company Site').save
Webhost.new(:domain=>'www.teststore.example',
:description=>'A Test Store').save
Webhost.new(:domain=>'www.smallblog.example',
:description=>'A Small Blog').save
You could also use the following ActiveRecord::Extensions code:
Webhost.import([:domain, :description],
[['www.somecompany.example, 'Some Company Site'],
['www.teststore.example', 'A Test Store'],
['www.smallblog.example', 'A Small Blog']]
This method saves the time required to create the Active Record object, which can be
very significant. It also runs just one query rather than three. In other words, the first
Active Record example produces queries like this:
INSERT INTO webhosts (domain, description) ('www.somecompany.example',
'Some Company Site');
INSERT INTO webhosts (domain, description) ('www.


Pages:
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
M.I.A. Brian McKnight Megadeth fajna muza mapa filmy w sieci