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?