0

わかりました、これが私が持っているものです

wines テーブルの列-> ID、 price テーブルの Name
列-> ID、wine_id、price

@query = Wine.find(:all)

カスタム価格列を @query ハッシュに追加するにはどうすればよいですか? これはできますか?

最終的に欲しいのはこれ

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


編集:

実際のテーブル構造

ワインには多くの在庫があります

インベントリーには 1 つのワイン
インベントリーには 1 つのゾーン
インベントリーには多くのアベイラビリティーがあります

可用性には 1 つの在庫
可用性には 1 つの Markupprofile_id があります
可用性には 1 つの 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. 価格は問題ありません。通常の価格です。問題は、

この割引価格 ( f.stock = ((was_price.markup.to_f / 100) + 1) * @price * 6 )このコレクションに of_type float 列を「追加」する方法はありますか? float 型の discounted_price 列を考えてみましょう。これはできますか?



4

4 に答える 4

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 %>
于 2012-04-18T10:10:43.940 に答える
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 %>
于 2012-04-18T10:39:01.073 に答える
0

両方のクラスの間に関連性はありますか?

class Wine < ActiveRecord
  has_one :price
end

class Price < ActiveRecord
  belongs_to :wine
end

次に、次のように呼び出すだけです。

<% @query.each do |e| %>
  <%= e.price.price %>
<% end %>
于 2012-04-18T10:11:36.577 に答える
0

attr_accessor を使用してこれを達成できると思います!

を見てみましょう:

Rails での attr_accessor の使用

Ruby の attr_accessor、attr_reader、attr_writer を使用する理由

于 2012-04-18T11:30:55.590 に答える