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')
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 |
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 |