class Player < ActiveRecord::Base
belongs_to :team
end
class Team < ActiveRecord::Base
has_many :players
end
схема: ActiveRecord :: Schema.define (версия: 20150125183356) сделатьПочему моя ассоциация работает неправильно?
create_table "players", force: :cascade do |t|
t.string "player_name"
t.float "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "team_id"
end
create_table "teams", force: :cascade do |t|
t.string "team_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
это сценарий
Когда я иду к рельсам консоль и введите
t= Team.new
t.team_name='ss'
t.save
p=Player.new
p.player_name='sdeas'
p.price=33.0
p.save
тогда, когда я печатаю p.team = t , он отлично работает.
, но когда я типа t.players он показывает мне #<ActiveRecord::Associations::CollectionProxy []>
Я хочу найти игроков для этой команды. Я уверен, что я ошибаюсь, когда я новичок в рельсах.
Работает ли он после ввода 't.reload'? –
yes, t.reload возвращает значения t – user3420043
Теперь попробуйте 't.players' .. –