# Why's my course\_duration being reset

**URL:** <https://www.ruby-forum.com/t/whys-my-course-duration-being-reset/147356>\
**Category:** Rails\
**Created:** [August 31, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/whys-my-course-duration-being-reset/147356 "2008-08-31T23:47:44Z")\
**Posts on this page:** 7\
**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 31, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/whys-my-course-duration-being-reset/147356/1 "2008-08-31T23:47:44Z")

</div>

Can’t get this?..I’m close I think, my calculation appears to be  
working, but… when i access duration = \<%= @enquiry.course\_duration  
%\>  
 in my final view it’s blank… course\_duration is getting set to  
null but works for the calculation?

## controller…

class EnquiriesController \< ApplicationController

layout ‘welcome’

before\_filter :login\_required, :except =\> [:new, :create, :blank]

def index  
list  
render :action =\> ‘list’  
end

# GETs should be safe (see

[URIs, Addressability, and the use of HTTP GET and POST](http://www.w3.org/2001/tag/doc/whenToUseGet.html))  
verify :method =\> :post, :only =\> [:destroy, :create, :update],  
:redirect\_to =\> { :action =\> :list }

def list  
@enquiries = Enquiry.find(:all, :order =\> ‘created\_at ASC’)  
end

def list\_by\_start\_date  
@enquiries = Enquiry.find(:all, :order =\> ‘requested\_start\_date  
ASC’)  
end

def list\_not\_responded  
# @enquiries = Enquiry.find(:all, :conditions =\> ‘responded = null’,  
:order =\> ‘requested\_start\_date ASC’)  
# entity = MyEntity.find\_all\_by\_active(true)  
@enquiries = Enquiry.find\_all\_by\_responded(false)  
end

def show  
@enquiry = Enquiry.find(params[:id])  
end

def new

```
@enquiry = Enquiry.new
@courses = Course.active_courses
@accomodations = Accomodation.active_accomodation
# @enquiry.course_booking_fee = Course.registration_fee

render :layout => "welcome"

```

end

def create  
@enquiry = Enquiry.new(params[:enquiry])

```
@courses = Course.active_courses
@accomodations = Accomodation.active_accomodation

if @enquiry.save

 @enquiry.set_fixed_fees # set the fixed fees
 @enquiry.calculate_fees # calculate

 @enquiry.save

  flash[:notice] = 'Your enquiry has been sent to Europa School of

```

English. You have received a summary by email. We will contact you  
shortly with further information.’  
# redirect\_to :action =\> ‘list’ rmoved rf, we need to go somewhere  
else after a new enq.

```
  redirect_to :action => 'blank', :id => @enquiry

  # redirect_to :action => "blank"
  Notifier.deliver_signup_thanks(@enquiry)

else
  render :action => 'new'

end

```

end

def edit  
@enquiry = Enquiry.find(params[:id])  
@courses = Course.find(:all)  
@accomodations = Accomodation.find(:all)

end

def update  
@enquiry = Enquiry.find(params[:id])  
if @enquiry.update\_attributes(params[:enquiry])  
flash[:notice] = ‘Enquiry was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @enquiry  
else  
render :action =\> ‘edit’  
end  
end

def destroy  
Enquiry.find(params[:id]).destroy  
redirect\_to :action =\> ‘list’  
end

def blank  
@enquiry = Enquiry.find(params[:id])

end

end

## model

class Enquiry \< ActiveRecord::Base

belongs\_to :course  
belongs\_to :accomodation

attr\_accessor :accomodation\_duration, :course\_duration

# before\_validation :set\_fixed\_fees, :calculate\_fees

def set\_fixed\_fees

```
# set accommodation_booking_fee from the Selected Accomodation
# set course_booking_fee from the Selected Course

self.course_booking_fee = course.registration_fee
self.accommodation_booking_fee = accomodation.booking_fee

```

end

def calculate\_fees

```
# DO STUFF !
# self.blah_fee = blah_duration * whatever

self.course_fee = course.price_per_week * course_duration.to_i
self.accommodation_fee = accomodation.price_per_week *

```

accomodation\_duration.to\_i

```
@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.5
  end

self.course_fee = (self.course_fee * @factor)

self.total_fee = course_booking_fee + accommodation_booking_fee +

```

course\_fee + accommodation\_fee

end

validates\_date :date\_of\_birth, :requested\_start\_date

validates\_presence\_of :title,  
:firstname,  
:surname,  
:address1,  
:phone,  
:country,  
:nationality,  
:course\_duration,  
:english\_level,  
:course\_id,  
:accomodation\_id,  
:message =\> “can’t be blank”

validates\_format\_of :email, :with =\> RFC2822::EmailAddress

end

---

<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:** [September 1, 2008, 12:41am UTC](https://www.ruby-forum.com/t/whys-my-course-duration-being-reset/147356/2 "2008-09-01T00:41:21Z")

</div>

On 31 Aug 2008, at 22:47, bingo bob wrote:

> Can’t get this?..I’m close I think, my calculation appears to be  
> working, but… when i access duration = \<%= @enquiry.course\_duration  
> %\>  
> in my final view it’s blank… course\_duration is getting set to  
> null but works for the calculation?

Hard to say since you haven’t said what view is involved (top tip -  
pasting 200 lines of code isn’t the best way to get people to read  
your question. try to only include the relevant bits).  
Are you under the impression that course\_duration will be save to the  
database? it won’t (it’s just a regular instance variable rather then  
an activerecord attribute).

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:** [September 1, 2008, 7:19am UTC](https://www.ruby-forum.com/t/whys-my-course-duration-being-reset/147356/3 "2008-09-01T07:19:25Z")

</div>

Sorry Fred,

Noted re the tip… here’s a few lines from the view…

\<%= @enquiry.course.name %\>  
  
base price per week = \<%= @enquiry.course.price\_per\_week %\>  
  
duration = \<%= @enquiry.course\_duration %\>  
 \<— The Line that’s  
null  
@enquiry.course\_booking\_fee : \<%= @enquiry.course\_booking\_fee %\>

and yes, I thought course\_duration would be saved, or to put it another  
way, I’d like it to be?

---

<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:** [September 1, 2008, 10:02am UTC](https://www.ruby-forum.com/t/whys-my-course-duration-being-reset/147356/4 "2008-09-01T10:02:13Z")

</div>

On 1 Sep 2008, at 06:19, bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> and yes, I thought course\_duration would be saved, or to put it  
> another  
> way, I’d like it to be?

Then it needs to be a column on your enquiries table.

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:** [September 1, 2008, 1:31pm UTC](https://www.ruby-forum.com/t/whys-my-course-duration-being-reset/147356/5 "2008-09-01T13:31:21Z")

</div>

On 1 Sep 2008, at 12:25, bingo bob wrote:

> > Then it needs to be a column on your enquiries table.
> > 
> > Fred
> 
> It is…that’s why I don’t get it?  
> enquiries contains a column called “course\_duration” of type integer.

In that case the problem is that you’ve got  
attr\_accessor :course\_duration. This is replacing the accessor methods  
that read/write from the database column with one that don’t (ie just  
plain old ruby accessor methods backed by an instance variable)

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:** [September 1, 2008, 1:25pm UTC](https://www.ruby-forum.com/t/whys-my-course-duration-being-reset/147356/6 "2008-09-01T13:25:22Z")

</div>

Frederick C. wrote:

> On 1 Sep 2008, at 06:19, bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > and yes, I thought course\_duration would be saved, or to put it  
> > another  
> > way, I’d like it to be?
> 
> Then it needs to be a column on your enquiries table.
> 
> Fred

It is…that’s why I don’t get it?  
enquiries contains a column called “course\_duration” of type integer.

---

<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:** [September 1, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/whys-my-course-duration-being-reset/147356/7 "2008-09-01T20:41:36Z")

</div>

Frederick C. wrote:

> On 1 Sep 2008, at 12:25, bingo bob wrote:
> 
> > > Then it needs to be a column on your enquiries table.
> > > 
> > > Fred
> > 
> > It is…that’s why I don’t get it?  
> > enquiries contains a column called “course\_duration” of type integer.
> 
> In that case the problem is that you’ve got  
> attr\_accessor :course\_duration. This is replacing the accessor methods  
> that read/write from the database column with one that don’t (ie just  
> plain old ruby accessor methods backed by an instance variable)
> 
> Fred

you got it, thanks.
