# Maths not working (can't get this)

**URL:** <https://www.ruby-forum.com/t/maths-not-working-cant-get-this/146791>\
**Category:** Rails\
**Created:** [August 25, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/maths-not-working-cant-get-this/146791 "2008-08-25T14:36:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 25, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/maths-not-working-cant-get-this/146791/1 "2008-08-25T14:36:18Z")

</div>

I’ve got a few problems with this…Hopefully it’s cleaer what I’m  
trying to do (work out and display two prices, one discounted / one  
full). Be grateful for answers…

The total price and discounted price are not calculated correctly and  
the duration is not coming through to the output page.

* * *

class Course \< ActiveRecord::Base

has\_one :enquiry

attr\_accessor :duration\_in\_weeks

def self.all\_courses  
find(:all)  
end

def self.active\_courses  
# find(:all)  
# find(:all, :conditions =\> [“active = ?”, true], :order =\>  
‘created\_at DESC’, :limit =\> 5)  
find(:all, :conditions =\> {:archive =\> false}, :order =\>  
‘display\_order ASC’, :limit =\> 100)  
end

def self.old\_courses  
# find(:all)  
# find(:all, :conditions =\> [“active = ?”, true], :order =\>  
‘created\_at DESC’, :limit =\> 5)  
find(:all, :conditions =\> {:archive =\> true}, :order =\> ‘created\_at  
DESC’, :limit =\> 100)  
end

def discounted\_price  
factor = case @duration\_in\_weeks  
when 1…4  
1.0  
when 5…12  
0.9  
when 13…24  
0.8  
when 25…47  
0.7  
else # from 48 and up  
0.6  
end

```
total_price * factor

```

end

def total\_price  
(@duration\_in\_weeks \* price\_per\_week) + registration\_fee  
end

def duration\_in\_weeks=(duration\_in\_weeks)  
@duration\_in\_weeks = duration\_in\_weeks.to\_i  
end

end

## CONTROLLER

class InvoiceController \< ApplicationController

def index  
@enquiry = Enquiry.new  
@courses = Course.active\_courses  
@courses = Course.active\_courses  
render :layout =\> “welcome”  
end

def price

# if params[:id]

# if params[:course[:id]]

if params[:course][:id].empty?

```
flash[:notice] = 'You must select a course.'
redirect_to :action => "index"

```

else

```
if params[:course][:course_duration].empty?

  flash[:notice] = 'You must select a duration.'
  redirect_to :action => "index"

else

  @time = Time.now
  # @selected_course = Course.find_by_id(params[:course][:id])
  @duration = params[:course][:course_duration]
  # @total_price = (@duration.to_i *

```

@selected\_course.price\_per\_week) + @selected\_course.registration\_fee

```
  # @course = Course.find(params[:id])
  # @selected_course.discounted_price

  @course = Course.find(params[:course][:id])
  @course.duration_in_weeks = params[:course][:duration]
  # @course.discounted_price <- not needed here.

  render :layout => "welcome"

end

```

end

end

end

* * *

INPUT PAGE

\<% form\_tag :action =\> ‘price’ do %\>

Select a course in the box below  
  
\<%= collection\_select(:course, :id, @courses, :id, :name, options  
={:prompt =\> “-Select a course”}, :class =\>“course”) %\>  
  
Select a duration (weeks) in the box below  
  
\<%= select(‘course’, ‘course\_duration’, 1…52, { :include\_blank =\> true  
} ) %\>  
  
\<%= submit\_tag “Proceed to quotation” %\>  
\<% end %\>

* * *

OUTPUT PAGE

* * *

# \<%= @course.name %\>

* * *
  
\<%= @time.to\_formatted\_s(:long\_ordinal) %\>  
  
Price per week: \<%= number\_to\_currency(@course.price\_per\_week, :unit =\> "£", :separator =\> ".", :delimiter =\> ",") %\>  
Duration: \<%= @course.duration\_in\_weeks %\> weeks  
Registration Fee: \<%= number\_to\_currency(@course.registration\_fee, :unit =\> "£", :separator =\> ".", :delimiter =\> ",") %\>  
  
 

Total Price: \<%= number\_to\_currency(@course.total\_price, :unit =\> "£", :separator =\> ".", :delimiter =\> ",") %\>

Discounted Price: \<%= number\_to\_currency(@course.discounted\_price,  
:unit =\> “£”, :separator =\> “.”, :delimiter =\> “,”) %\>

  
Please note that this price is indicative and not a binding quotation. The price excludes any fees for accommodation, travel and examinations. Terms and conditions apply.  
  

\<%= button\_to “Return to pricing page”, :action =\> “index” %\>

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 25, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/maths-not-working-cant-get-this/146791/2 "2008-08-25T14:45:39Z")

</div>

On 25 Aug 2008, at 13:36, bingo bob wrote:

> I’ve got a few problems with this…Hopefully it’s cleaer what I’m  
> trying to do (work out and display two prices, one discounted / one  
> full). Be grateful for answers…

It’s basically impossible to read this because there are almost more  
commented out lines than actual lines.  
That said you’re checking whether params[:course][:course\_duration]  
is empty but then you’re using params[:course][:duration] instead.

Fred

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 25, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/maths-not-working-cant-get-this/146791/3 "2008-08-25T15:13:12Z")

</div>

Frederick C. wrote:

> On 25 Aug 2008, at 13:36, bingo bob wrote:
> 
> > I’ve got a few problems with this…Hopefully it’s cleaer what I’m  
> > trying to do (work out and display two prices, one discounted / one  
> > full). Be grateful for answers…
> 
> It’s basically impossible to read this because there are almost more  
> commented out lines than actual lines.  
> That said you’re checking whether params[:course][:course\_duration]  
> is empty but then you’re using params[:course][:duration] instead.
> 
> Fred

You got it thanks…

Now I’ve decided I want to access factor in my view…

but it’s blank when i try   
Factor: \<%= @course.factor %\>

here’s my model…

class Course \< ActiveRecord::Base

has\_one :enquiry

attr\_accessor :duration\_in\_weeks  
attr\_reader :factor

def self.all\_courses  
find(:all)  
end

def self.active\_courses  
# find(:all)  
# find(:all, :conditions =\> [“active = ?”, true], :order =\>  
‘created\_at DESC’, :limit =\> 5)  
find(:all, :conditions =\> {:archive =\> false}, :order =\>  
‘display\_order ASC’, :limit =\> 100)  
end

def self.old\_courses  
# find(:all)  
# find(:all, :conditions =\> [“active = ?”, true], :order =\>  
‘created\_at DESC’, :limit =\> 5)  
find(:all, :conditions =\> {:archive =\> true}, :order =\> ‘created\_at  
DESC’, :limit =\> 100)  
end

def discounted\_price  
@factor = case @duration\_in\_weeks  
when 1…4  
1.0  
when 5…12  
0.9  
when 13…24  
0.8  
when 25…47  
0.7  
else # from 48 and up  
0.6  
end

```
total_price * @factor

```

end

def total\_price  
(@duration\_in\_weeks \* price\_per\_week) + registration\_fee  
end

def duration\_in\_weeks=(duration\_in\_weeks)  
@duration\_in\_weeks = duration\_in\_weeks.to\_i  
end

end
