I have:
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