ADVANCED ACTIVE RECORD: ENHANCING YOUR MODELS in Font

Generating QR in Font ADVANCED ACTIVE RECORD: ENHANCING YOUR MODELS

CHAPTER 5 ADVANCED ACTIVE RECORD: ENHANCING YOUR MODELS
QR Code Maker In None
Using Barcode printer for Font Control to generate, create QR image in Font applications.
www.OnBarcode.com
Draw QR-Code In None
Using Barcode drawer for Font Control to generate, create QR Code 2d barcode image in Font applications.
www.OnBarcode.com
Table 5-3. Common has_one Options Option
GTIN - 12 Encoder In None
Using Barcode creator for Font Control to generate, create UPCA image in Font applications.
www.OnBarcode.com
Encoding EAN 128 In None
Using Barcode creator for Font Control to generate, create GS1-128 image in Font applications.
www.OnBarcode.com
:class_name
Making PDF 417 In None
Using Barcode drawer for Font Control to generate, create PDF-417 2d barcode image in Font applications.
www.OnBarcode.com
Creating Code39 In None
Using Barcode generator for Font Control to generate, create Code 39 image in Font applications.
www.OnBarcode.com
Description
Draw Code 128 Code Set C In None
Using Barcode creation for Font Control to generate, create Code 128 Code Set B image in Font applications.
www.OnBarcode.com
Print Monarch In None
Using Barcode drawer for Font Control to generate, create Ames code image in Font applications.
www.OnBarcode.com
Specifies the class name of the association. Used when the class name can t be inferred from the association name. Specifies the conditions that the associated object must meet in order to be included as a WHERE SQL fragment. Specifies the foreign key used for the association in the event that it doesn t adhere to convention of being the lowercase, singular name of target class with _id appended. Specifies the order from which the associated object will be picked as an ORDER BY SQL fragment. Specifies that the associated object should be removed when this object is. If set to :destroy, the associated object is deleted using the destroy method. If set to :delete, the associated object is deleted without calling its destroy method. If set to :nullify, the associated object s foreign key is set to NULL.
QR Code 2d Barcode Creation In None
Using Barcode maker for Online Control to generate, create QR Code image in Online applications.
www.OnBarcode.com
Recognize QR Code In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Example
EAN13 Encoder In Objective-C
Using Barcode creator for iPad Control to generate, create EAN 13 image in iPad applications.
www.OnBarcode.com
PDF417 Drawer In None
Using Barcode printer for Software Control to generate, create PDF417 image in Software applications.
www.OnBarcode.com
has_one :location, :class_name => 'Address'
Barcode Generation In None
Using Barcode generation for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Code39 Generation In Objective-C
Using Barcode generation for iPhone Control to generate, create Code 3/9 image in iPhone applications.
www.OnBarcode.com
:conditions
Making QR Code ISO/IEC18004 In None
Using Barcode drawer for Microsoft Word Control to generate, create QR Code image in Microsoft Word applications.
www.OnBarcode.com
Recognize QR Code 2d Barcode In Visual Studio .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
has_one :address, :conditions => "active = 1"
Generating QR Code In None
Using Barcode maker for Excel Control to generate, create Quick Response Code image in Office Excel applications.
www.OnBarcode.com
Scan Code 128 Code Set B In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
:foreign_key
Generate Code 128B In Java
Using Barcode maker for BIRT reports Control to generate, create Code128 image in Eclipse BIRT applications.
www.OnBarcode.com
EAN-13 Supplement 5 Scanner In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
has_one :address, :foreign_key => 'location_id'
:order
has_one :address, :order => "created_at DESC"
:dependent
has_one :address, :dependent => :destroy
Creating One-to-Many Associations
One-to-many associations describe a pattern where a row in one table is related to one or more rows in another table. Examples would be an Email that has many Recipients, or a Magazine that has many Subscriptions. It s time to say good-bye to contrived examples and put these principles to work in our events application.
Adding the User Model
When we first started the events application, we decided to let anyone create new events. This worked fine when there was only one person using the system, but we want this to
CHAPTER 5 ADVANCED ACTIVE RECORD: ENHANCING YOUR MODELS
be a multiple-user application and let different people sign up, sign in, and start managing their own events, separately from one another. Up until now, our events have been orphaned they don t belong to anyone. We re going to remedy that now by creating a User model and associating users with events. In our system, each event is going to belong to a user, and a user may have many events. Figure 5-3 illustrates this association.
Figure 5-3. The one-to-many relationship between users and events
Let s fire up the generator and create the User model now.
$ ./script/generate model User
Just as you saw in the previous chapter, the model generator creates, among other things, a model file in app/models and a migration in db/migrate. Open db/migrate/ 003_create_users.rb and give it the schema definition shown in Listing 5-2 (add the following code to the file).
Listing 5-2. Migration to Create the users Table, db/migrations/003_create_users.rb
class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.column :login, :string t.column :email, :string t.column :password, :string end # Add a foreign key reference to users in the events table add_column :events, :user_id, :integer end
CHAPTER 5 ADVANCED ACTIVE RECORD: ENHANCING YOUR MODELS
def self.down drop_table :users remove_column :events, :user_id end end
This is standard migration fare. In the self.up definition, we used the create_table method to create a new users table. The new table object is yielded to the block in the variable, t, on which we call the column method to create each row. Along with the standard login and email fields, we re specifying a hashed_password field, which we ll use for authentication, as we ll explain in the Reviewing the Updated Models section later in this chapter. The primary key, id, will be created automatically, so there s no need to specify it here. Pay particular attention to the user_id column in the self.up section. It s the foreign key reference column we ve been talking so much about. Also note that its type is :integer. That s important, because it s referring to a numeric id. Now all we need to do is run the migration and create the table using the db:migrate Rake task. Run the migration with the following command:
$ rake db:migrate
== CreateUsers: migrating ===================================================== -- create_table(:users) -> 0.0035s -- add_column(:events, :user_id, :integer) -> 0.0065s == CreateUsers: migrated (0.0103s) ============================================
With the table and foreign keys in place, Listings 5-3 and 5-4 show how we declare the one-to-many association in our Event and User models. Add these to the relevant models now.
Copyright © OnBarcode.com . All rights reserved.