2012-04-18 27 views
0

好吧,這裏是我的葡萄酒表添加自定義列紅寶石收集

列 - > 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列?這可以做到嗎?

+0

你能不能用attr_accessor? [使用attr訪問器在Rails](http://stackoverflow.com/questions/2793098/usage-of-attr-accessor-in-rails)或 [爲什麼使用ruby attr訪問器,讀寫器]( http://stackoverflow.com/questions/5046831/why-use-rubys-attr-accessor-attr-reader-and-attr-writer) – 2012-04-18 11:25:39

+0

讓我看看attr accessor – Francois 2012-04-18 11:29:26

+0

它適合你嗎? – 2012-04-21 07:42:44

回答

2

讓我們假設你有葡萄酒和價格

之間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 %> 
+0

你比我快28秒:-) – auralbee 2012-04-18 10:12:54

+0

@auralbee你如何計算秒之差? – 2012-04-18 10:15:48

+0

這是我剛剛打出「添加答案」後給我的區別 – auralbee 2012-04-18 10:20:12

0

您是否在兩個類之間存在關聯?

class Wine < ActiveRecord 
    has_one :price 
end 

class Price < ActiveRecord 
    belongs_to :wine 
end 

然後,你可以直接電話諮詢:

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

嗨,會爲你發佈實際的表格結構,是的,我有關係 – Francois 2012-04-18 10:16:00

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 %>