Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Scaffold to automatically do one-to-many relationship

Not sure if I'm reading this right, but it seems like Scaffold will not do a one-to-many relationship in its entirety. For instance, if I create messages with scaffold and then I want comments on those messages (one message -> many comments), I have to go through and change everything. For instance, I have to change this in the comment's new view

<% form_for(@comment) do |f| %>

to this

<% form_for([@message, @comment]) do |f| %>

and then change the Action to set up the @message var... amongst other things.

This cannot currently be done automatically with Scaffold, right?

like image 698
Dan Rosenstark Avatar asked Feb 02 '09 03:02

Dan Rosenstark


People also ask

What is scaffolding in Ruby on rails?

Ruby on Rails - Scaffolding. While you're developing Rails applications, especially those which are mainly providing you with a simple interface to data in a database, it can often be useful to use the scaffold method. Scaffolding provides more than cheap demo thrills.

Should I use scaffolding or rails for web development?

But Rails is nice. You can create individual components, like controllers, using the rails g (g for generate) command. One of the big benefits of using a scaffolding command is that all the files are created using the correct naming conventions, which avoids strange error messages.

How do I declare a many-to-many relationship between models in rails?

Rails offers two different ways to declare a many-to-many relationship between models. The first way is to use has_and_belongs_to_many, which allows you to make the association directly: class Assembly < ApplicationRecord has_and_belongs_to_many :parts end class Part < ApplicationRecord has_and_belongs_to_many :assemblies end

What is an example of a model in a Rails application?

For example, consider a simple Rails application that includes a model for authors and a model for books. Each author can have many books. Without associations, the model declarations would look like this:


2 Answers

Yes. Scaffold works for a model and related controller. It does not take care of or work with relationships.

Scaffold's primary objective is to get CRUD going on a model using a controller and related views. That's all. Any other requirement like relationships has to be coded manually.

like image 106
Chirantan Avatar answered Oct 08 '22 04:10

Chirantan


This is true, but, it's not the end of the story. There are at least two alternatives to Scaffold that both work quite well and automatically pick up on relationships between classes (based on your ActiveRecord relationship indicators like has_many). One of these alternatives is Streamlined and the other is ActiveScaffold.

They're mainly helpful for entering in data that your system requires that is not user entered data. For example, I use them for administrative tasks on tables where there's no point in building a complete UI for CRUD when one of the scaffold alternatives will do the job just fine for a seldom used feature. You wouldn't want to use them for comments on messages though.

like image 26
John Munsch Avatar answered Oct 08 '22 03:10

John Munsch