# One-to-many from script/generate scaffold \<model\> \<controller\>

**URL:** <https://www.ruby-forum.com/t/one-to-many-from-script-generate-scaffold-model-controller/122671>\
**Category:** Rails\
**Created:** [December 10, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/one-to-many-from-script-generate-scaffold-model-controller/122671 "2007-12-10T15:45:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 10, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/one-to-many-from-script-generate-scaffold-model-controller/122671/1 "2007-12-10T15:45:13Z")

</div>

I followed a tutorial step by step which resulted in what has been,  
for  
me, the holy grail: a one-to-many relationship. A screenshot of what  
I  
ended up with:

Name: Chili Cookoff

Budget: 150.0  
Edit | Back  
Itemized Expenses  
2007-12-09 Fire Department $25.00  
2007-12-10 Parties R Us $75.00

[http://expenses.googlecode.com/files/chili\_cookoff.png](http://expenses.googlecode.com/files/chili_cookoff.png), clearly  
demonstrating the “many” expenses per accounts; a “one-to-many”  
relationship with CRUD interface.

The step which was most mysterious in the tutorial:

"Generate the scaffolding by typing

$ script/generate scaffold account expenses

That command generates a number of files for us; files we’d rather  
not  
create by hand. The first parameter (account) specifies the name of  
the  
model, which is generated in the app/models/account.rb file. The  
second  
parameter (expenses) specifies the name of the controller, which is  
generated in the app/controllers/expenses\_controller.rb file. The  
scaffold generator also created template files for the views of our  
application."

[http://developer.apple.com/tools/rubyonrails.html](http://developer.apple.com/tools/rubyonrails.html)

This is all well and good, but the choice for the singular versus  
plural  
choice is unclear to me. Both models are singular, as expected.  
Why,  
then, is it “expenses” and not “expense” in the above scaffold  
command?  
Is this significant, a typo, or is the name of the scaffold here  
immaterial?

It strikes me that there’s an intuitive mismatch between navigating  
to  
[http://localhost:3000/expenses](http://localhost:3000/expenses) and being presented with a listing  
of  
accounts:

Listing accounts  
Name Budget  
Chili Cookoff 150.0 Show Edit Destroy  
Car Wash 25.0 Show Edit Destroy  
Pancake Breakfast 125.0 Show Edit Destroy  
Dunk the Programmer 50.0 Show Edit Destroy

New account

rather than a listing of expenses.

Which brings me to my next question: what’s the connection between  
the  
model name, Expense, and the name of this particular controller,  
expenses\_controller.rb? Could it have been “script/generate scaffold  
account foo”, or was it critical that “expenses” match up with the  
Expense model?

Additionally:

"$ rake migrate

Next we need an Expense model to wrap the expenses table. Use the  
generator to create the model by typing

$ script/generate model expense

That command gives us an empty Expense model class. Now we have to  
tell Rails that a relationship exists between the accounts and  
expenses tables. (Rails can’t accurately derive relationships simply  
by looking at the database schema.) Specifically, an expense belongs  
to an account. Declare that relationship in the Expense model by  
updating the app/models/expense.rb file as follows:

class Expense \< ActiveRecord::Base  
belongs\_to :account  
end"

Ok, the above makes sense, except for: why the choice to run “script/  
generate model” versus they initially ran “script/generate migration”?

For the Accounts model they used a migration, but for expenses they  
use “generate model”; why?

thanks,

Thufir
