2011-01-12 66 views
0

我:节能家长不救孩子

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 

回答

0
def put(test, price) do 
    ttp = TariffTestPrice.first_or_create(:tariff => self, :test => test) 
    ttp.price = price 
    ttp.save 
end 
+0

我知道那。但我想通过节省关税来节省关税测试价格。 – guest 2011-01-13 10:20:15