# Basic MVC in RoR question

**URL:** <https://www.ruby-forum.com/t/basic-mvc-in-ror-question/101438>\
**Category:** Rails\
**Created:** [February 6, 2006, 3:22am UTC](https://www.ruby-forum.com/t/basic-mvc-in-ror-question/101438 "2006-02-06T03:22:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Basic\_MVC\_in\_RoR\_qu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@Basic\_MVC\_in\_RoR\_qu](https://www.ruby-forum.com/u/Basic_MVC_in_RoR_qu)\
**Post date:** [February 6, 2006, 3:22am UTC](https://www.ruby-forum.com/t/basic-mvc-in-ror-question/101438/1 "2006-02-06T03:22:22Z")

</div>

Ok, Here’s my controller:

def create  
trainingplan = Trainingplan.createplan(@params[:startdate],  
@params[:enddate], @params[:hoursperweek], @params[:programtype])

end

My model is:

class Trainingplan \<ActiveRecord::Base  
def self.createplan(stardate, enddate, hoursperweek, programtype)  
stardate = startdate  
enddate = enddate  
hoursperweek = hoursperweek  
programtype = programtype  
end  
end

My View is:

\<%= @trainingplan.startdate %\>

What I’m trying to do is add “annualhours” which is just  
hoursperweek\*52.

So I add to my model:

annualhours = hoursperweek\*52

However, this gives me a nil object error.

I know this is simple and I’ve read the Agile book but still I’m not  
getting it.

What exactly do I need to add to my controller or model and view in  
order to calculate and display the annualhours?

---

<div class="post-metadata">

**Author:** ![Basic\_MVC\_in\_RoR\_qu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@Basic\_MVC\_in\_RoR\_qu](https://www.ruby-forum.com/u/Basic_MVC_in_RoR_qu)\
**Post date:** [February 6, 2006, 3:57am UTC](https://www.ruby-forum.com/t/basic-mvc-in-ror-question/101438/2 "2006-02-06T03:57:39Z")

</div>

On 2/5/06, Basic MVC in RoR question [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My View is:
> 
> annualhours = hoursperweek\*52
> 
> However, this gives me a nil object error.
> 
> I know this is simple and I’ve read the Agile book but still I’m not  
> getting it.
> 
> What exactly do I need to add to my controller or model and view in  
> order to calculate and display the annualhours?

Er, annualhours is probably a local variable in the model, so it goes  
away at the end of the method call. That’s why you don’t see it in  
the view.

What you want is a new method called annualhours (although it should  
be called annual\_hours).

So:

class TrainingPlan \< AR  
def annual\_hours  
self.hoursperweek \* 52  
end  
end

---

<div class="post-metadata">

**Author:** ![Basic\_MVC\_in\_RoR\_qu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@Basic\_MVC\_in\_RoR\_qu](https://www.ruby-forum.com/u/Basic_MVC_in_RoR_qu)\
**Post date:** [February 6, 2006, 4:20am UTC](https://www.ruby-forum.com/t/basic-mvc-in-ror-question/101438/3 "2006-02-06T04:20:38Z")

</div>

> Er, annualhours is probably a local variable in the model, so it goes  
> away at the end of the method call. That’s why you don’t see it in  
> the view.
> 
> What you want is a new method called annualhours (although it should  
> be called annual\_hours).
> 
> So:
> 
> class TrainingPlan \< AR  
> def annual\_hours  
> self.hoursperweek \* 52  
> end  
> end

annualhours is part of the model as it is a column in the database.  
Therefore I assumed it would be part of Trainingplan. I don’t  
understand why it would be local to the model and not available in the  
view? I was under the impression that instance variables like  
@annualhours were local to the model, controller, or view and variables  
that were part of the model were accessible in the view. So if I have  
annualhours defined as a part of the trainingplan table, shouldn’t I be  
able in the model to do:

annualhours = hoursperweek\*52

and in the view do:

@trainingplan.annualhours

This however doesn’t work.

---

<div class="post-metadata">

**Author:** ![Basic\_MVC\_in\_RoR\_qu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@Basic\_MVC\_in\_RoR\_qu](https://www.ruby-forum.com/u/Basic_MVC_in_RoR_qu)\
**Post date:** [February 6, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/basic-mvc-in-ror-question/101438/4 "2006-02-06T15:26:14Z")

</div>

Basic MVC in RoR question wrote:

> and in the view do:
> 
> @trainingplan.annualhours
> 
> This however doesn’t work.

If annualhours is a column in the table, I believe you need to do  
self.annualhours within the model class in order to set or access its  
value.

> class Trainingplan \<ActiveRecord::Base  
> def self.createplan(stardate, enddate, hoursperweek, programtype)  
> stardate = startdate  
> enddate = enddate  
> hoursperweek = hoursperweek  
> programtype = programtype  
> end  
> end

Also, what you’re doing here isn’t actually creating a row in the  
database. At  
no time do you create a new TrainingPlan class, and you don’t make a  
call to  
that objects save method. So I think you need to go back to your  
controller:

> def create  
> trainingplan = Trainingplan.createplan(@params[:startdate],  
> @params[:enddate], @params[:hoursperweek], @params[:programtype])  
> end

And change this code to something more like this:

def create  
trainingplan = TrainingPlan.create (  
:stardate =\> @params[:startdate],  
:enddate =\> @params[:enddate],  
:hoursperweek =\> @params[:hoursperweek],  
:annualhours =\> @params[:hoursperweek].to\_i \* 52,  
:programtype =\> @params[:programtype])  
end

-Brian

---

<div class="post-metadata">

**Author:** ![Basic\_MVC\_in\_RoR\_qu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@Basic\_MVC\_in\_RoR\_qu](https://www.ruby-forum.com/u/Basic_MVC_in_RoR_qu)\
**Post date:** [February 6, 2006, 4:58am UTC](https://www.ruby-forum.com/t/basic-mvc-in-ror-question/101438/5 "2006-02-06T04:58:04Z")

</div>

Can you post more complete code from the controller, model, and view? It  
sounds like it should work more or less, so it could be some subtle  
problem  
in your code. I’ve noticed, for instance, a few typos above and I’m not  
sure  
if that is your actual code or you re-typed it.

- byron
