Why's my course_duration being reset

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

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

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?

On 1 Sep 2008, at 06:19, bingo bob [email protected]
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

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

Frederick C. wrote:

On 1 Sep 2008, at 06:19, bingo bob [email protected]
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.

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.