У меня проблема с отношением «многие ко многим» в Rails 3. Здесь задействованы 3 модели:
class Device < ActiveRecord::Base
attr_accessible :name, :protocol, :address, :status, :room_id
belongs_to :room
has_many :device_abilities
has_many :abilities, :through => :device_abilities
end
class Ability < ActiveRecord::Base
attr_accessible :name
has_many :device_abilities
has_many :devices, :through => :device_abilities
end
class DeviceAbility < ActiveRecord::Base
belongs_to :device
belongs_to :ability
end
, и у меня есть следующее в моем начальном файле для тестирования:
device_abilities = DeviceAbility.create([
{ device_id: '1', ability_id: '1'},
{ device_id: '1', ability_id: '2'},
{ device_id: '1', ability_id: '3'},
{ device_id: '1', ability_id: '4'},
{ device_id: '1', ability_id: '5'},
{ device_id: '2', ability_id: '1'},
{ device_id: '2', ability_id: '2'},
{ device_id: '2', ability_id: '3'},
{ device_id: '2', ability_id: '4'},
{ device_id: '2', ability_id: '5'}
])
(реальные устройства и способности также определены, и я могу нормально с ними взаимодействовать).
Я получаю необычную ошибку при попытке сделать
def show
@device = Device.find(params[:id])
puts @device.abilities
end
(put - для отладки; я также не могу использовать это, если помещаю это в переменную).Ошибка:
undefined method `zero?' for nil:NilClass
Может кто-нибудь сказать мне, где я иду не так?
обновление:
по запросу, трассировка ошибки:
app/controllers/devices_controller.rb:13:in `puts'
app/controllers/devices_controller.rb:13:in `puts'
app/controllers/devices_controller.rb:13:in `show'
и схема:
create_table "abilities", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "devices", :force => true do |t|
t.string "name"
t.string "protocol"
t.string "address"
t.boolean "status"
t.integer "room_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "device_abilities", :force => true do |t|
t.integer "device_id"
t.integer "ability_id"
t.datetime "created_at"
t.datetime "updated_at"
end