# Can someone explain RoR MVC from a C++ or Java perspective?

**URL:** <https://www.ruby-forum.com/t/can-someone-explain-ror-mvc-from-a-c-or-java-perspective/100739>\
**Category:** Rails\
**Created:** [July 31, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/can-someone-explain-ror-mvc-from-a-c-or-java-perspective/100739 "2006-07-31T20:25:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbqDude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@bbqDude](https://www.ruby-forum.com/u/bbqDude)\
**Post date:** [July 31, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/can-someone-explain-ror-mvc-from-a-c-or-java-perspective/100739/1 "2006-07-31T20:25:43Z")

</div>

is a model similiar to a class? and active record is similiar to how  
Java classes inherit from Object?

View is nothing more than an output of a model with HTML?

Controller is basically the Main part of the program?

---

<div class="post-metadata">

**Author:** ![bbqDude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@bbqDude](https://www.ruby-forum.com/u/bbqDude)\
**Post date:** [July 31, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/can-someone-explain-ror-mvc-from-a-c-or-java-perspective/100739/2 "2006-07-31T20:42:38Z")

</div>

Yes, model is just a class. It is the domain object. Active record gives  
you the ORM functionality  
to your model classes. You just extend your model class from  
ActiveRecord class.

View is similar to jsp pages. It has a template that can be .rhtml with  
ruby code embedded in  
html. Think of Controller class as the Action class in Struts. Welcome  
to the wonderful  
wonderland!!!

---

<div class="post-metadata">

**Author:** ![bbqDude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@bbqDude](https://www.ruby-forum.com/u/bbqDude)\
**Post date:** [July 31, 2006, 8:46pm UTC](https://www.ruby-forum.com/t/can-someone-explain-ror-mvc-from-a-c-or-java-perspective/100739/3 "2006-07-31T20:46:46Z")

</div>

bbqDude wrote:

> is a model similiar to a class? and active record is similiar to how  
> Java classes inherit from Object?
> 
> View is nothing more than an output of a model with HTML?
> 
> Controller is basically the Main part of the program?

Controllers and models are all Classes, and every instance of any object  
inherits form class Object in Ruby.

Controller:  
Handles requests and glues Models and Views together. When a request  
comes in the controller has two responsibilities. Change the state of  
models, if necesary, and retrieve data that the view will need.  
“actions” each have a unique url, and each action is a public method of  
a particular controller object. So for controller People and the urls  
“/people”, “people/show/1”, “people/edit/1” the controller would have  
the public methods index, show, and edit.

The more code that can be put in the model, rather than the controller,  
the better. For example:

#Bad:  
def charge\_order  
order = Order.find(params[:id])  
result = CreditAPI.charge(1234123412341234, 9.99)  
if result.success?  
order.staus = :charged  
order.save  
end  
end

#Good:  
def charge\_order  
Order.find(params[:id])  
order.charge!(1234123412341234, 9.99)  
end

Model:  
Wraps a particular database table, each column in the database gets an  
attribute reader/writer so you can do “@post.title = ‘foo’” and whatnot.  
A model may also be a Class that does not inherit form ActiveRecord,  
such as a ShoppingCart which simply exists in the session until an order  
is saved.

View:  
Renders HTML (or RJS, or XML, etc.) based on the instance variable  
defined in the controller action that processed the request. For  
instance, in the controller:

class BlogController \< ApplicationController  
def index  
@posts = Post.find(:all)  
end  
end

And say you wanted a list of the titles of all the posts. You would  
have a view files called /app/views/blog/index.rhtml with the following:

 \<% for post in @posts %\> - \<%= post.title %\>
 \<% end %\>

---

<div class="post-metadata">

**Author:** ![bbqDude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@bbqDude](https://www.ruby-forum.com/u/bbqDude)\
**Post date:** [July 31, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/can-someone-explain-ror-mvc-from-a-c-or-java-perspective/100739/4 "2006-07-31T20:55:33Z")

</div>

Hello Alex,  
Thanks for an awesome explanation! I was just having trouble on what  
belongs in a model vs in a controller. Thanks for putting things in  
perspective!

---

<div class="post-metadata">

**Author:** ![bbqDude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@bbqDude](https://www.ruby-forum.com/u/bbqDude)\
**Post date:** [August 1, 2006, 1:43am UTC](https://www.ruby-forum.com/t/can-someone-explain-ror-mvc-from-a-c-or-java-perspective/100739/5 "2006-08-01T01:43:48Z")

</div>

you said put as much as you can in the model, why is this? and where is  
the  
demarkation in your controller actions that should be split into the  
model  
and controller, i.e what is the rule of thumb for putting actions in the  
model?

#Bad:  
def charge\_order  
order = Order.find(params[:id])  
result = CreditAPI.charge(1234123412341234, 9.99)  
if result.success?  
order.staus = :charged  
order.save  
end  
end

#Good:  
def charge\_order  
Order.find(params[:id])  
order.charge!(1234123412341234, 9.99)  
end

---

<div class="post-metadata">

**Author:** ![bbqDude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@bbqDude](https://www.ruby-forum.com/u/bbqDude)\
**Post date:** [August 1, 2006, 1:56am UTC](https://www.ruby-forum.com/t/can-someone-explain-ror-mvc-from-a-c-or-java-perspective/100739/6 "2006-08-01T01:56:32Z")

</div>

On 7/31/06, bbqDude [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello Alex,  
> Thanks for an awesome explanation! I was just having trouble on what  
> belongs in a model vs in a controller. Thanks for putting things in  
> perspective!

It would probably help to read up about the MVC design pattern in  
either of the books /Head First Design Patterns/ or /Design Patterns/.  
First explains the pattern in Java, and the other one explains the  
pattern in C++ (I think).

Joe
