将自定义列添加到ruby集合

时间:2012-04-18 10:01:51

标签: ruby-on-rails ruby activerecord collections

好的,这就是我所拥有的

葡萄酒表中的

列 - > ID,名称
价格表中的列 - > ID,wine_id,价格

@query = Wine.find(:all)

现在如何在@query哈希中添加自定义价格列?可以这样做吗?

这就是我想要的结果

<% @query.each do |e| %>
  <%= e.price %>
<% end %>

<小时/> 修改

实际表格结构

  

葡萄酒有很多库存

     

库存有一个酒   库存有一个区域
  库存有很多可用性

     

可用性有一个库存
  可用性有一个Markupprofile_id
  可用性有一个Discountprofile_id

这是我遵循的过程

#special offers
@q_array = Array.new
@q = SpecialOffers.find(:all, :conditions => ["client_id = ? AND application_id = ?", @client_id, @app_id])
@q.each do |e|
    @q_array << e.availability_id
end

#filter, Special Offers, Client App Id, Zone, Available
@special_offers = Wine.find(:all, :include => [:availabilities, :zones], :conditions => ["availabilities.available = ? AND availabilities.id IN (?) AND availabilities.client_application_id = ? AND zones.id = ?", true, @q_array, @client_app_id, session[:zone_id]], :order => 'wines.name ASC')
#search page = 1, new arrivals = 2, special offers = 3, best sellers = 4, show page = 5
add_markup(@special_offers, 3)

现在我有我想要的葡萄酒,我通过 add_markup()

运行它们
def add_markup(collection, unique)

@price_array ||= [] #if null create blank array

collection.each do |e|
e.inventories.each do |f|
if session[:zone_id] == f.zone_id

@availability = Availability.find_by_inventory_id_and_client_application_id(f.id, @client_app_id)
@price = f.price

if @availability
if @availability.discount == true
price = Pricingmodel.find_by_discountprofile_id(@availability.discountprofile_id)
if price
was_price = Pricingmodel.find_by_markupprofile_id(@availability.markupprofile_id)

f.price = ((price.markup.to_f / 100) + 1) * @price * 6 #this is the normal price
f.stock = ((was_price.markup.to_f / 100) + 1) * @price * 6 #this is the discounted price
else
price = Pricingmodel.find_by_markupprofile_id(@availability.markupprofile_id)

f.price = ((price.markup.to_f / 100) + 1) * @price * 6 #this is the normal price
f.stock = ((price.markup.to_f / 100) + 1) * @price * 6 #this is the discounted price
end
else
price = Pricingmodel.find_by_markupprofile_id(@availability.markupprofile_id)

f.price = ((price.markup.to_f / 100) + 1) * @price * 6 #this is the normal price
f.stock = ((price.markup.to_f / 100) + 1) * @price * 6 #this is the discounted price
end
end

end
end
end
end

f.price很好,这是正常价格。

问题是,我想在某处显示折扣价

我在这个折扣价格中使用了int类型的库存栏(f.stock =((was_price.markup.to_f / 100)+ 1)* @price * 6)

有什么办法可以“添加”一个of_type float列到这个集合吗?让我们说float类型的discounted_price列?可以这样做吗?

4 个答案:

答案 0 :(得分:2)

我们假设您在Wine和Price

之间存在has_one关联

然后

class Wine < ActiveRecord::Base
  has_one :price
end 
class Price < ActiveRecord::Base
   belongs_to :wine
end

然后在您的视图中显示如下价格。

<% @query.each do |e| %>
  <%= e.price.price %>
<% end %>

答案 1 :(得分:0)

两个班级之间是否有关联?

class Wine < ActiveRecord
  has_one :price
end

class Price < ActiveRecord
  belongs_to :wine
end

然后你可以简单地打电话:

<% @query.each do |e| %>
  <%= e.price.price %>
<% end %>

答案 2 :(得分:0)

我建议您使用delegate

class Wine < ActiveRecord::Base
  has_one :price
  delegate :rate, :to => :price # Notice 'rate' instead of 'price'. So that you could still get the associated price record.
end

class Price < ActiveRecord::Base
  belongs_to :wine
end

你可以简单地做

<% @query.each do |e| %>
  <%= e.rate %>
<% end %>

答案 3 :(得分:0)

我认为你可以使用attr_accessor来实现这个目标!

看看:

usage of attr_accessor in Rails

Why use Ruby's attr_accessor, attr_reader and attr_writer?