7

I am new to Ruby on Rails. So far, I have only created CRUD operations by using scaffolding. Now, I need to integrate two entities into single form using scaffolding, not by hard coding it.

How can we scaffold two entities together using script?

I have two entities, Student_address and Student_phnumber. I want to scaffold these two entities into a single form where I can do CRUD operations, and I want to achieve this by scaffolding.

Student_address is an entity consisting of Hse_name, Street_name, etc. Student_phnumber another entity consisting of ph_number, type, etc.

I want to scaffold these two entities together.

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
Okky
  • 10,338
  • 15
  • 75
  • 122

3 Answers3

6

Scaffolding is nothing more than a generator set up to model a complete basic resource. There are many other generators, other than scaffolding, that come with Rails by default. None of them are set up to generate resources for a set of related models. I suspect that a large part of this is because of the wide range of methods to express such a relationship make creating a generic UI virtually impossible. Also, scaffolding is more set up to get you up and running very quickly, with the intention of being modified to suit your needs. These modifications are usually fairly involved for any non-trivial application. There are many 3rd party generators out there, including the popular nifty generators, but none that create the kind of code you want to generate, as far as I know. If this is a relationship that you need to set up frequently, you may consider creating a generator of your own to handle the task - it's actually pretty easy. A good way to do it is to implement your ideal case, then create a generator from it.

Edit:

Also, you may wish to adjust your variable/attribute names. They should be lowercase and underscored, so Street_name would become street_name. Arbitrary abbreviations also make it very hard to code/maintain, so Student_phnumber would be better expressed as student_phone_number. The reason for doing this, apart from consistency (ph_number vs Student_phnumber, for example), is that Rails actually uses the casing and spacing in internal methods like these.

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
2

Let me see if I understand you.

The model/entity relationship you are describing is:

student
   address
    - house_name
    - street_name
    - etc
   phone_number
    - number
    - area_code
    - etc

You want to:

a) automatically generate the models

b) automatically generate a controller/view with a form to create a student, including fields to set up the address and phone number

Okay. b) can't be done via a Rails scaffold. You can, however, use the ActiveSupport gem (docs here) to achieve this. Here's what you do:

gem install active_scaffold
rails g active_scaffold Student name:string
rails g active_scaffold PhoneNumber area_code:integer number:integer student_id:integer
rails g active_scaffold Address first_line:string second_line:string student_id:integer

The only manual work you'll have to do here is pop into the models and add the relationships:

Address
 belongs_to :student
PhoneNumber
 belongs_to :student
Student
 has_one :address
 has_one :phone_number

What ActiveScaffold will do is automatically produce for you a view like this:

enter image description here

Fill in that form and your models will all be saved and linked together!

Will Hamilton
  • 442
  • 6
  • 15
0

Is this what you are looking for?

rails generate scaffold Student student_address:string student_phnumber:string

Someth Victory
  • 4,492
  • 2
  • 23
  • 27
  • no Student_address is an entity consisting of Hse_name, Street_name, etc. Student_phnumber another entity consisting of ph_number, type, etc. I want to scaffold these two entities together – Okky Oct 29 '12 at 04:29