foreign keys & rails...

20
Foreign Keys & Rails Associations Arthur V. Klepchukov 02.13/2oo8

Upload: others

Post on 22-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Foreign Keys & Rails Associations

Arthur V. Klepchukov02.13/2oo8

Page 2: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

let’s start a start-up!

myfacespace

Page 3: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Lets add ProfilePicsBETA

users:

•id

•name

•email

•...

•etc.

We need... users!

Page 4: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Iteration 1:: naiveusers:

•id•name•email•...•profile_picture_path

Page 5: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Iteration 2 ::1-1 relationship

users: (has_one :profile_pic)

•...

profile_pics: (belongs_to :user)•id•user_id # foreign key to user!•picture_path•hot_or_not

Page 6: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Iteration 2 ::1-1 relationship

class ProfilePic < ActiveRecord::Basebelongs_to :user

end

1 pic is boring!Let’s add PhotoAlbumsBETA

profile pics

id

user_id

...

users

id

name

...

class User < ActiveRecord::Basehas_one :profile_pic

end

Page 7: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Iteration 1 :: naive

users:

•...•album_title•album_public•album_being_watched_by_CIA

UGLY!only ONE photo album

per user!!!

Page 8: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Iteration 2 ::1-M relationship

users: (has_many :albums)

•...

albums: (belongs_to :user)•id•user_id # foreign key to user!•title•public•being_watched_by_CIA

Page 9: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Iteration 2 ::1-M relationship

class Album < ActiveRecord::Basebelongs_to :user

end

albums

id

user_id

...

users

id

name

...

class User < ActiveRecord::Basehas_many :albums

end

Let’s add groupsBETA

Page 10: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Iteration 1 :: naiveusers:

•...•group1_name•group1_revolutionary_mission_statement

•...•groupN_name•groupN_revolutionary_mission_statement

Right... ?

NO!!!very ugly!

Page 11: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Iteration 2 ::M-M relationship

users: (has_and_belongs_to_many :groups)

• ...

groups: (has_and_belongs_to_many :users)

• id

• name

• revolutionary_mission_statement

groups_users: <<< join table, name matters (in Rails)

• group_id # foreign key to group!

• user_id # foreign key to user!

Page 12: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Iteration 2 ::M-M relationship

class Group < ActiveRecord::Basehas_and_belongs_to_many :users

end

groups

idmission_statement

...

users

id

name

...

class User < ActiveRecord::Basehas_and_belongs_to_many :groups

end

groups_usersgroup_iduser_id

Notice: no corresponding model!

Page 13: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Why bother?

•Logical Organization

•Avoid functional dependencies

•Easier to write queries in general

•Rails got your back- helps you avoid some join queries!

Page 14: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Simple Join QuerySELECT u.name, p.hot_or_not FROM users u INNER JOIN profile_pics p ON u.id = p.user_id;

Give me the name and hot_or_not status of each user with a profile picture.

Page 15: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Ugly Join QuerySELECT * FROM users u INNER JOIN groups_users gu ON u.id = gu.user_id INNER JOIN groups g ON gu.group_id = g.id LEFT OUTER JOIN profile_pictures p ON u.id = p.user_id WHERE g.name LIKE “%Ron Obama%” AND p.picture_path == NULL;

:(

Page 16: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Rails got my back?@user = User.find(:first)@groups = @user.groups # all of the groups this user belongs to

@user.profile_pic.hot_or_not = "NOT!" # makes this user ugly as far as the internet is concerned

@user.albums.each { |album| raise HorribleException if album.name =~ /NSFW/ } # your boss is looking...

Page 17: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Important details

•@user.save # DON’T FORGET! required for the changes on the previous slide to actually take effect (more on this later)

•@users = User.find( :all, :include => [:albums, :groups] ) # vital optimization if dealing with users and their albums and their groups

Page 18: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Remember

•Foreign keys are important in keeping a sane DB

•Rails makes working with typical relationships easy

•Much, much more to learn:- Agile Web Dev :: Ch. 18- CS 186

Page 19: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Special ThanksSQL & Prof. Joe Hellerstein

Page 20: Foreign Keys & Rails Associationsdb.ucsd.edu/cse190/slides/Foreign_Keys_and_Rails_Associations.pdf · 1-1 relationship users: (has_one :profile_pic) ... •picture_path •hot_or_not

Next step: VC funding