Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 计量单位换算_Ruby On Rails 3_Gem_Ruby On Rails Plugins_Units Of Measurement - Fatal编程技术网

Ruby on rails 3 计量单位换算

Ruby on rails 3 计量单位换算,ruby-on-rails-3,gem,ruby-on-rails-plugins,units-of-measurement,Ruby On Rails 3,Gem,Ruby On Rails Plugins,Units Of Measurement,对于我的Rails项目,我正在寻找一个可以转换质量、体积和其他单位的库 我需要把千克转换成克,把升转换成汤匙等等 我想,应该是这样的: class Product < ActiveRecord:Base acts_as_physical_unit, :volume, :mass, :count end class Ingredient < ActiveRecord:Base acts_as_physical_unit, :volume, :mass, :count end

对于我的Rails项目,我正在寻找一个可以转换质量、体积和其他单位的库

我需要把千克转换成克,把升转换成汤匙等等

我想,应该是这样的:

class Product < ActiveRecord:Base
  acts_as_physical_unit, :volume, :mass, :count
end

class Ingredient < ActiveRecord:Base
  acts_as_physical_unit, :volume, :mass, :count
end

olive_oil = Product.new(:name => "Olive Oil", :volume => "1000 ml")

caesar_salad = Recipe.new(:name => "Caesar salad",
  :ingredients => [
    Ingredient.new(:product => [olive_oil], :volume => "5 tablespoons")
  ]

# In ingredients of "Caesar Salad" are 5 tablespoons of "Olive Oil" specified.
# We should create "Caesar Salad" for 50 persons.
# How mutch bottles of "Olive Oil" should be ordered ?
order = OrderItem.new(
  :product => olive_oil,
  :count => olive_oil.count_for(caesar_salad.ingredients.first)) * 50
)
class产品“橄榄油”,体积=>“1000毫升”)
凯撒沙拉=配方。新(:name=>“凯撒沙拉”,
:配料=>[
成分。新(:产品=>[橄榄油],:体积=>“5汤匙”)
]
#“凯撒沙拉”的配料中有5大汤匙的“橄榄油”。
#我们应该为50人制作“凯撒沙拉”。
#应该订购多少瓶“橄榄油”?
order=OrderItem.new(
:产品=>橄榄油,
:count=>橄榄油。计数(凯撒沙拉。配料。第一次))*50
)
这样的宝石真的存在吗

谢谢。

您可能想试试:

你可以看看这个是否适合你

你可以用它来做这件事。它是为单位转换和计量数学而设计的,用于大量的科学单位。它不了解Rails,但连接起来应该相当容易:

require 'unitwise/ext'

class Ingredient < ActiveRecord::Base
  # store the value as milliliter in the database
  def volume=(amount)
    super(amount.convert_to('ml').to_f)
  end

  # convert the value to a measurement when retrieved
  def volume
    super().convert_to('ml')
  end
end

# Now you can initialize by sending a number (assumed to be in ml)
cooking_wine = Ingredient.new(name: 'Cooking Wine', volume: 750)

# Or send any other volume measurement
olive_oil = Ingredient.new(name: 'Olive Oil', volume: 1.liter)
cumin = Ingredient.new(name: 'Cumin', volume: 5.teaspoon)

# Now volume returns a measurement
cooking_wine.volume # => #<Unitwise::Measurement 750.0 ml>

# And the volume can be converted to whatever you want.
olive_oil.volume.convert_to('gallon') # => #<Unitwise::Measurement 0.21996924829908776 gallon>

# Use .to_f to get just the numeric value
cumin.volume.convert_to('cup').to_f # => 0.10416666666666666

# You can perform whatever math you need on the volumes as well:
2.0 * (olive_oil.volume + cooking_wine.volume) => #<Unitwise::Measurement 3500.0 ml>
需要“单位/外部”
类组件#
#卷可以被转换成你想要的任何东西。
橄榄油。体积。换算成('galler')#=>#
#使用.to\u f仅获取数值
孜然。体积。将_转换为('cup')。转换为_f=>0.104166666
#您还可以对卷执行所需的任何数学运算:
2.0*(橄榄油体积+料酒体积)=>#

漂亮的宝石。感谢您在Rails的ActiveRecord模型中共享使用情况。@Josh如果我的用户在输入音量时可以选择测量类型,我需要如何更改该音量=方法?谢谢你的帮助@MicFin,我在这里写了更多关于这个话题的内容: