Chapter 5 - Working with Active Record Flashcards

1
Q

Rails Active Record

A

The Rails Active Record framework includes mechanisms for representing models and their relationships, CRUD (create, read, update, and delete) operations, complex searches, validation, callbacks, and many more features. It relies heavily on convention over configuration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In order to create a new model class, the first thing you do is?

A

In order to create a new model class, the first thing you do is to declare it as a subclass of ActiveRecord:: Base.

class Client

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

CRUD

A

Create, Read, Update, and Delete.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

new_record?

A

Returns true if this object hasn’t been saved yet – that is, a record for the object doesn’t exist in the database yet; otherwise, returns false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

persisted?

A

Returns true if the record is persisted, i.e. it’s not a new record and it was not destroyed, otherwise returns false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Active Record create

A

c = Client.create( name: “Nile River, Co.”, code: “NRC”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Reading Active Record Objects

A

first_project = Project.find( 1)

all_clients = Client.all

first_client = Client.first

Product.last

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

String versus Symbol

A

Many Rails methods accept symbol and string parameters interchangeably, and that is potentially very confusing. Which is more correct? The general rule is to use symbols when the string is a name for something and a string when it’s a value. You should probably be using symbols when it comes to keys of options, hashes, and the like.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Hash Notation

A

first_client[: name]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Attributes method

A

first_client.attributes

Being able to grab a hash of all attributes at once is useful when you want to iterate over all of them or pass them in bulk to another function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

attribute_before_type_cast accessors

A

def fix_rate
self[: rate] =
rate_before_type_cast.tr(‘ $,’,’’)
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Updating

A

class ProjectController

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Touching Records

A

> > user = User.first

> > user.touch 
# = > sets updated_at to now. 
> > user.touch(: viewed_at)
# sets viewed_at and updated_at to now.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Delete and Destroy

A

The delete method executes a SQL statement to remove the object’s data from the database . The destroy method will both remove the object from the database and prevent you from
modifying it again:

> > bad_timesheet = Timesheet.find( 1)

> > bad_timesheet.destroy

Timesheet.delete(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Locking

A

Locking is a term for techniques that prevent concurrent users of an application from overwriting each other’s work.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Where clause

A

Product.where( sku: params[: sku])

17
Q

where.not

A

Article.where.not( title: ‘Rails 3’)

requires pure string

18
Q

where boolean

A

Timesheet.where(‘ submitted = ?’, true)

19
Q

where nil

A

> > User.where(: email = > nil)

User Load (15.2ms) SELECT

20
Q

Order clause

A

Timesheet.order(:created_at)

21
Q

Having clauses

A

> > User.group(“ created_at”). having([” created_at > ?”, 2. days.ago])

22
Q

readonly

A

c = Comment.readonly.first

23
Q

reverse_order

A

Member.order(: name). reverse_order