The save! method will raise a RecordNotSaved exception
if the save was not successful.
25
C H A P T E R 2
?– ?– ?–
It??™s also possible to create objects in one swift stroke using the create method. This
method works almost exactly like new plus save. If it is possible for Active Record to save the
object with the attributes you??™ve passed in, it will. If it can??™t, it won??™t. In either case, the object
is returned and ready for normal use. This is useful if you have an object that can easily be
instantiated in one line, for example:
bookmark = Bookmark.create(:url => "http://www.apress.com", :name => "Apress")
There are also some dynamic methods for creating objects, but we??™ll discuss those when
we get to the finder methods.
?– Note When Active Record translates the value of an attribute into the data that will be used in the SQL
query, the value is given to the connection-specific adapter for translation into the format that the database
expects. For example, giving a date or time field a Ruby Time object will convert the value into YYYYMMDD
hh:mm:ss format when used with a MySQL database.
Pages:
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102