Skip to content

Instantly share code, notes, and snippets.

@ProfAvery
Created May 8, 2012 01:44
Show Gist options
  • Save ProfAvery/2631912 to your computer and use it in GitHub Desktop.
Save ProfAvery/2631912 to your computer and use it in GitHub Desktop.
Associations
$ rvm use 1.9.2@railstutorial
$ rails new movies
$ cd movies
$ bundle install --local
$ rails generate scaffold Movie title:string rated:string
$ rails generate scaffold Category name:string
$ rails generate migration create_categories_movies
db/migrate/*_create_categories_movies.rb:
1 class CreateCategoriesMovies < ActiveRecord::Migration
2 def self.up
3 create_table :categories_movies, :id => false do |t|
4 t.references :category
5 t.references :movie
6 end
7 end
8
9 def self.down
10 drop_table :categories_movies
11 end
12 end
$ bundle exec rake db:migrate
app/models/movie.rb:
1 class Movie < ActiveRecord::Base
2 has_and_belongs_to_many :categories
3 end
$ rails console
>> m = Movie.create :title => 'The Black Hole', :rated => 'PG'
>> scifi = Category.create :name => 'Science Fiction'
>> m.categories
>> m.categories << scifi
>> m.categories
>> m = Movie.create :title => 'Terminator', :rated => 'R'
>> m.categories << scifi
>> action = Category.create :name => 'Action'
>> m.categories << action
app/models/category.rb:
1 class Category < ActiveRecord::Base
2 has_and_belongs_to_many :movies
3 end
$ rails console
>> scifi = Category.find_by_name 'Science Fiction'
>> action = Category.find(2)
>> action.movies
>> scifi.movies
$ rails generate scaffold User name:string
$ rails generate scaffold Rating user_id:integer movie_id:integer stars:integer
$ bundle exec rake db:migrate
app/models/user.rb:
1 class User < ActiveRecord::Base
2 has_many :ratings
3 end
app/models/rating.rb:
1 class Rating < ActiveRecord::Base
2 belongs_to :user
3 end
$ rails console
>> alice = User.create :name => 'Alice'
>> bob = User.create :name => 'Bob'
>> alice.ratings.create :movie_id => 2, :stars => 3
>> bob.ratings.create :movie_id => 1, :stars => 3
>> bob.ratings.create :movie_id => 2, :stars => 5
>> Rating.first.user
>> Movie.find(Rating.first.movie_id)
app/models/movie.rb:
1 class Movie < ActiveRecord::Base
2 has_and_belongs_to_many :categories
3 has_many :ratings
4 end
app/models/rating.rb:
1 class Rating < ActiveRecord::Base
2 belongs_to :user
3 belongs_to :movie
4 end
$ rails console
>> User.first.ratings.first.movie
>> Movie.find(2).ratings.first.user.name
>> bob = User.find(2)
>> bob.ratings.map { |rating| rating.movie }
app/models/user.rb:
1 class User < ActiveRecord::Base
2 has_many :ratings
3 has_many :movies, :through => :ratings
4 end
$ rails console
>> bob = User.find(2)
>> bob.movies
>> bob.movies.create :title => 'Alien', :rated => 'R'
>> bob.movies
>> bob.ratings
>> bob.ratings.last.stars = 5
>> bob.ratings.last.save
>> terminator = Movie.find 2
>> terminator.ratings
>> terminator.ratings.first.user
app/models/movie.rb:
1 class Movie < ActiveRecord::Base
2 has_and_belongs_to_many :categories
3 has_many :ratings
4 has_many :users, :through => :ratings
5 end
$ rails console
>> terminator = Movie.find 2
>> terminator.users
$ rvm use 1.9.2@railstutorial
$ rails new quiz
$ cd quiz
$ bundle install --local
$ rails generate scaffold Question text:text
$ rails generate scaffold Answer question_id:integer text:string correct:boolean
$ bundle exec rake db:migrate
$ rails console
>> q = Question.create :text => 'Routes map from URLs to'
>> Answer.create :question_id => q.id, :text => 'Views', :correct => false
>> Answer.create :question_id => q.id, :text => 'Actions', :correct => true
>> Answer.create :question_id => q.id, :text => 'Layouts', :correct => false
>> Answer.create :question_id => q.id, :text => 'Partials', :correct => false
>> q = Question.create :text => 'Views are written in'
>> Answer.create :question_id => q.id, :text => 'HAML', :correct => true
>> Answer.create :question_id => q.id, :text => 'ERB', :correct => true
>> Answer.create :question_id => q.id, :text => 'HTML', :correct => false
>> Answer.create :question_id => q.id, :text => 'PHP', :correct => false
>> Answer.find_all_by_question_id(q.id)
>> a = Answer.find(2)
>> Question.find(a.question_id)
app/models/question.rb:
1 class Question < ActiveRecord::Base
2 has_many :answers
3 end
>> reload!
>> q = Question.find(1)
>> q.answers
app/models/answer.rb:
1 class Answer < ActiveRecord::Base
2 belongs_to :question
3 end
>> a = Answer.find(2)
>> a.question
>> Answer.find_by_correct(true)
>> Answer.find_by_question_id_and_correct(q.id, true)
>> Answer.where('correct = true')
>> Answer.where('correct = ?', true)
app/models/answer.rb:
1 class Answer < ActiveRecord::Base
2 belongs_to :question
3 scope :correct, where('correct = ?', true)
4 end
>> reload!
>> Answer.correct
>> q = Question.find(1)
>> q.answers
>> q.answers.correct
>> Question.where('text like ?', '%view%')
app/models/question.rb:
1 class Question < ActiveRecord::Base
2 has_many :answers
3 scope :containing, lambda { |string| where('text like ?', "%#{string}%") }
4 end
>> Question.containing('view')
>> Question.containing('route')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment