Active Record Flashcards
find_in_batches
Yields each batch of records that was found by the find options as an array.:batch_size - Specifies the size of the batch. Default to 1000.:start - Specifies the starting point for the batch processing.
find_each
The find_each method uses find_in_batches with a batch size of 1000
column_names
Returns an array of column names as strings.
column_defaults
Returns a hash where the keys are column names and the values are default values when instantiating the AR object for this table.
User.where(‘name != ?’, ‘Gabe’)
Qureries the database for all Users where the name not Gabe
User.where(‘name = ? AND last_name != ?’,’Gabe’,’Smith’)
Queries the database for all Users where the name is Gabe and the last name is not Smith
User.where(‘age < 45’)
Queries the database for all Users where the age is less than 45.
Person.destroy_all(conditions: nil)
Destroys records of the Person model matching conditions, or all if no condition is passed. Each object’s callbacks are executed.
Person.delete_all(conditions: nil)
Deletes the records matching conditions without instantiating the records first, and hence not calling the destroy method nor invoking callbacks.
Person.pluck(:name)
Returns an Array of attribute values type-casted to match the plucked column names, if they can be deduced.