• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/11

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

11 Cards in this Set

  • Front
  • Back
In Active Record what is a dynamic finder?
A method created on the fly by using find_by_, find_last_by_, or find_all_by_.

order = Order.find_by_name("Dave Thomas")
orders = Order.find_all_by_name("Dave Thomas")
orders = Order.find_all_by_email(params['email'])
In Active Record, what can you use to create a new object if one does not exist in the db?
If there isn’t a record in the database, and you want to create one, calling a method whose name starts find_or_initialize_by_
or find_or_create_by_ will call either new or create
What are Named and Anonymous scopes in Active Record?
A named scope can be associated with a Proc and therefore
may have arguments:

class Order < ActiveRecord::Base
named_scope :last_n_days, lambda { |days| :condition =>
['updated < ?', days] }
end
In Active Record what method is used to save changes / a new object to the database?
.save
In Active Record, what would we use to save changes on an object in the database with values from a form?
update_attributes method is most commonly used in controller actions where it merges data from a form into an existing database row:

def save_after_edit
order = Order.find(params[:id])
if order.update_attributes(params[:order])
redirect_to :action => :index
else
render :action => :edit
end
end
In Active Record, what does .update to?
The update method takes an id parameter and a set of attributes. It fetches the corresponding row, updates the given attributes, saves the result to the database, and returns the model object.

order = Order.update(12, :name => "Barney", :email => "barney@bedrock.com")
In Active Record, what do the two kinds of save do?
save returns true if the record was saved; it returns nil otherwise.

save! returns true if the save was successful; it raises an exception
otherwise.
In Active Record, what do the two kinds of create do?
create returns the Active Record object regardless of whether it was successfully saved. You’ll need to check the object for validation errors if you want to determine whether the data was written.

create! returns the Active Record object on success; it raises an exception otherwise.
What Active Record method do we use when we want to save a record?
If we need to save a model object in a context where we want to make sure that all errors are handled programmatically, we should use save!.
What are the two methods for deleting in Active Record. Which is preferred and why?
There is .delete and .destroy. Generally .destroy is preferred because it fires off all filters and validations before removing records.
What is the function that is used to enable composition in Active Record?
composed_of