app/views/pokemons/index.html.erb
app/controllers/pokemons_controller.rb
config/routes.rb
app/models/pokemon.rb
Pokemon.where('level > 30')
Pokemon.where(level: '> 30')
Pokemon.find(30)
@pokemon.level(30)
users | ||
---|---|---|
id | name | num_badges |
1 | Sam | 0 |
2 | Mark | 8 |
pokemons | |||
---|---|---|---|
id | name | level | description |
1 | Charmander | 35 | The best starter |
4 | Bulbasaur | 25 | The one no one chooses |
Pokemon.find(User.find(2))
Pokemon.where(id: 2)
Pokemon.where(User.name: 'Sam')
This is impossible!
users | ||
---|---|---|
id | name | num_badges |
1 | Sam | 0 |
2 | Mark | 8 |
pokemons | |||
---|---|---|---|
id | name | level | description |
1 | Charmander | 5 | The best starter |
4 | Bulbasaur | 2 | The one no one chooses |
pokemons | |||
---|---|---|---|
id | name | level | description |
1 | Charmander | 5 | The best starter |
4 | Bulbasaur | 2 | The one no one chooses |
users | ||
---|---|---|
id | name | num_badges |
1 | Sam | 0 |
2 | Mark | 8 |
How does a Pokemon know
which owner it belongs to?
pokemons | |||
---|---|---|---|
id | name | level | description |
1 | Charmander | 5 | The best starter |
4 | Bulbasaur | 2 | The one no one chooses |
users | ||
---|---|---|
id | name | num_badges |
1 | Sam | 0 |
2 | Mark | 8 |
pokemons | ||||
---|---|---|---|---|
id | user_id | name | level | description |
1 | 1 | Charmander | 5 | The best starter |
4 | 2 | Bulbasaur | 2 | The one no one chooses |
aka "foreign key"
belongs_to in Pokemon
has_many in User
@user.pokemons # [Pokemon(id: 2), ...]
@pokemon.user # User(id: 1)
http://guides.rubyonrails.org/association_basics.html
@user.pokemons
@user.pokemons.build(...)
@user.pokemons.size
@user = User.find(1) @pokemon = Pokemon.find(1) :belongs_to in Pokemon, :has_many in User
@pokemon.user
@user.pokemons
@user.pokemons.size
@user.pokemons.build(name: 'Ditto')
users | ||
---|---|---|
id | name | num_badges |
1 | Sam | 0 |
2 | Mark | 8 |
pokemons | |||
---|---|---|---|
id | name | level | description |
1 | Charmander | 5 | The best starter |
4 | Bulbasaur | 2 | The one no one chooses |
users | ||
---|---|---|
id | name | num_badges |
1 | Sam | 0 |
2 | Mark | 8 |
pokemons | ||||
---|---|---|---|---|
id | trainer_id | name | level | description |
1 | 1 | Charmander | 5 | The best starter |
4 | 2 | Bulbasaur | 2 | The one no one chooses |
has_many :pocket_monsters, class: "Pokemon", foreign_id: "trainer_id"
users (trainers) | ||
---|---|---|
id | name | num_badges |
1 | Sam | 0 |
2 | Mark | 8 |
gyms | ||
---|---|---|
id | name | num_badges |
1 | pewter city | 0 |
2 | veridian city | 8 |
users (trainers) | ||
---|---|---|
id | name | num_badges |
1 | Sam | 0 |
2 | Mark | 8 |
gyms | ||
---|---|---|
id | name | num_badges |
1 | pewter city | 0 |
2 | veridian city | 8 |
users (trainers) | ||
---|---|---|
id | name | num_badges |
1 | Sam | 0 |
2 | Mark | 8 |
gyms | ||
---|---|---|
id | name | num_badges |
1 | pewter city | 0 |
2 | veridian city | 8 |
user_gyms | ||
---|---|---|
id | gym_id | user_id |
1 | 1 | 1 |
2 | 1 | 2 |
has_many :user_gyms has_many :gyms, through: :user_gyms
has_many :user_gyms has_many :users, through: :user_gyms
belongs_to: user belongs_to: gym
In user.rb
In gym.rb
In user_gyms.rb