0

Ok, here is what I have

columns in wines table -> ID, Name
columns in price table -> ID, wine_id, price

@query = Wine.find(:all)

now how do I add a custom price column to the @query hash? Can this be done?

This is what I want in the end

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


EDIT:

Actual table structure

Wines has many Inventories

Inventories has one Wine
Inventories has one Zone
Inventories has many Availabilities

Availabilities has one Inventory
Availabilities has one Markupprofile_id
Availabilities has one Discountprofile_id

This is the process I follow

#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)

Now I have the wines I want and I run them through 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 is fine, its the normal price.

The problem is, I want to also display the discounted price somewhere

I used the stock column wich is of type int for this discounted price (f.stock = ((was_price.markup.to_f / 100) + 1) * @price * 6)

Is there any way I could "add" a of_type float column to this collection? Lets say discounted_price column of type float? Can this be done?

Francois
  • 10,465
  • 4
  • 53
  • 64
  • can't you use attr_accessor? [Usage of attr accessor in Rails](http://stackoverflow.com/questions/2793098/usage-of-attr-accessor-in-rails) or [Why use ruby attr accessor, reader and writer](http://stackoverflow.com/questions/5046831/why-use-rubys-attr-accessor-attr-reader-and-attr-writer) – Hardik Patel Apr 18 '12 at 11:25
  • let me have a look at attr accessor – Francois Apr 18 '12 at 11:29

4 Answers4

2

Let us assume you have has_one association between Wine and Price

Then

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

Then in your view display the price like below.

<% @query.each do |e| %>
  <%= e.price.price %>
<% end %>
Soundar Rathinasamy
  • 6,658
  • 6
  • 29
  • 47
0

Do you have an association between both classes?

class Wine < ActiveRecord
  has_one :price
end

class Price < ActiveRecord
  belongs_to :wine
end

Then you could simply call:

<% @query.each do |e| %>
  <%= e.price.price %>
<% end %>
auralbee
  • 8,741
  • 4
  • 29
  • 36
0

I'd suggest you to use 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

You could simply do

<% @query.each do |e| %>
  <%= e.rate %>
<% end %>
Waseem
  • 8,232
  • 9
  • 43
  • 54
0

I think you can achieve this using attr_accessor!

Take a look at:

usage of attr_accessor in Rails

Why use Ruby's attr_accessor, attr_reader and attr_writer?

Community
  • 1
  • 1
Hardik Patel
  • 838
  • 5
  • 12