Thufir
2007-Dec-10 14:44 UTC
one-to-many from script/generate scaffold <model> <controller>
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>, 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> 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> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---