Thin Controller Fat Model (and calculations)

I’m trying to elegantly add some functionality to my app. It’s a
language school that has an enquiry form. the functionality I am trying
to build is pricing. Basically I’d like to calculate a price for a
student and store it in the enquiry. Prices come in two flavours, Course
Price (course price per week * duration), Accommodation Price (accom
price per week * duration). There are one off booking fees for both.

So, how do I do all this maths in my Model (I read everywhere that the
model is the place to do it) and keep my controller clean.

Here’s the story so far.

Parameters passed when the form is submitted…

Parameters: {“commit”=>“Create”,
“enquiry”=>{“address1”=>“g”,
“country_living_in”=>“Germany”,
“date_of_birth(2i)”=>“1”,
“accomodation_duration”=>“4”,
“address2”=>“jkg”,
“date_of_birth(3i)”=>“1”,
“title”=>“Miss”,
“address3”=>“g”,
“postcode”=>“gj”,
“course_duration”=>“2”,
“body”=>“sdfds”,
“english_level”=>“Advanced”,
“accomodation_id”=>“1”,
“address4”=>“gj”,
“country”=>“Germany”,
“firstname”=>“sdf”,
“requested_start_date(1i)”=>“2009”,
“mobile”=>“gjk”,
“requested_start_date(2i)”=>“2”,
“phone”=>“jk”,
“requested_start_date(3i)”=>“3”,
“surname”=>“sdfdsfbjk”,
“course_id”=>“2”,
“email”=>“[email protected]”,
“nationality”=>“Germany”,
“date_of_birth(1i)”=>“1950”},
“action”=>“create”,
“controller”=>“welcome”}

My Enquiry Fields (the final price should eventually make it into
total_fee).

?> Enquiry.column_names
=> [“id”, “created_at”, “updated_at”, “title”, “firstname”, “surname”,
“responded”, “body”, “photo”, “phone”, “mobile”, “email”, “address1”,
“address2”, “address3”, “address4”, “postcode”, “country”,
“requested_start_date”, “date_of_birth”, “nationality”,
“country_living_in”, “english_level”, “accomodation_id”,
“accomodation_duration”, “course_id”, “course_duration”,
“course_booking_fee”, “course_fee”, “accommodation_booking_fee”,
“accommodation_fee”, “total_fee”]

==============

I started by doing the following stuff in my controllers create action

@accomodation_selected = Accomodation.find(

@enquiry.accomodation_id )
@course_selected = Course.find( @enquiry.course_id )

@enquiry.accommodation_booking_fee =

@accomodation_selected.booking_fee
@enquiry.course_booking_fee = @course_selected.registration_fee

This works fine to get me the values I need and save them to the DB (for
the one off fees), but I’m stuck on the next move, and I know that
really the complexity is growing and it should be in the model. Just not
sure how. A helping hand would be very much appreciated?

I think I need something like this, am I on the right lines?

My Proposed Enquiry model

class Enquiry < ActiveRecord::Base

belongs_to :course, :accomodation

attr_accessor :accomodation_duration, :course_duration

before_validation :set_fixed_fees, :calculate_fees

def set_fixed_fees

# set and save accommodation_booking_fee from the Selected 

Accomodation
# set and save course_booking_fee from the Selected Course

end

def calculate_fees

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

self.total_fee = 1111

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,
:message => “can’t be blank”

validates_format_of :email, :with => RFC2822::EmailAddress

end

Anyone? I’m stuck.

I think that this callback way is a good one.
But it’s better to make the validations first, no? You can use some
callback that do the things after validations. For example before_save
or before_create.

Here for more: ActiveRecord::Callbacks - APIdock