# Thin Controller Fat Model (and calculations)

**URL:** <https://www.ruby-forum.com/t/thin-controller-fat-model-and-calculations/147331>\
**Category:** Rails\
**Created:** [August 31, 2008, 9:41am UTC](https://www.ruby-forum.com/t/thin-controller-fat-model-and-calculations/147331 "2008-08-31T09:41:11Z")\
**Posts on this page:** 4\
**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, 9:41am UTC](https://www.ruby-forum.com/t/thin-controller-fat-model-and-calculations/147331/1 "2008-08-31T09:41:11Z")

</div>

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”=\>“removed\_email\_address@domain.invalid”,  
“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?

---

<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, 12:25pm UTC](https://www.ruby-forum.com/t/thin-controller-fat-model-and-calculations/147331/2 "2008-08-31T12:25:14Z")

</div>

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

---

<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, 1:32pm UTC](https://www.ruby-forum.com/t/thin-controller-fat-model-and-calculations/147331/3 "2008-08-31T13:32:58Z")

</div>

Anyone? I’m stuck.

---

<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, 4:54pm UTC](https://www.ruby-forum.com/t/thin-controller-fat-model-and-calculations/147331/4 "2008-09-01T16:54:26Z")

</div>

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](http://apidock.com/rails/ActiveRecord/Callbacks)
