У меня есть:
module App
module Data
class TariffTestPrice
include DataMapper::Resource
property :tariff_id, Integer, :key => true
property :test_id, Integer, :key => true
property :price, Float # domyślnie nil
belongs_to :tariff
belongs_to :test
end
end
end
module App
module Data
class Tariff
include DataMapper::Resource
property :id, Serial
property :name, String, :default => ''
has n, :tariff_test_prices
alias to_s name
def used?
!firms.empty?
end
def put(test, price)
ttp = tariff_test_prices.first_or_new(:test => test)
test.tariff_test_prices << ttp if ttp.dirty?
ttp.price = price
end
def at(test)
ttp = tariff_test_prices.first(:test => test)
ttp ? ttp.price : nil
end
end
end
end
module App
module Data
class Test
include DataMapper::Resource
property :id, Serial
property :name, String, :default => ''
has n, :tariff_test_prices
belongs_to :test_type, :model => 'TestType', :child_key => [ :type_id ]
alias type test_type
alias to_s name
def used?
!visits.empty? || !tariffs.empty?
end
private
def tariffs
tariff_test_prices.map &:tariff
end
end
end
end
Когда я ставлю тест значений, цену и сохраняю тариф, в базе данных ничего не меняется:
tariff.put(test, price)
tariff.save # It doesn't save TariffTestPrice