Reputation: 29
I have create controller
, model
and view
using scaffold with attributes:
org_name, org_description, webdomain, offc_addr, offc_ph
and my problem is that i want to create multiple address against single organization and multiple phone numbers against single address.
I am not confirm to use the following command
rails generate migration NAME [field:type field:type] [options]
as i just want to create a table and create association with other table not interested in creating View for that table and i am using nested form.
So please tell me whether is this appropriate or I do something else... Thanks in Advance.!
Upvotes: 1
Views: 2899
Reputation: 96
Before starting anything I given below you must remove any tables or controllers you created before and start it as a new and follow the instructions given below to avoid any exceptions or duplications.
So first you would need to generate a scaffold for Organizations and a simple model for PhoneNumbers and Addresses (this will generate its migration as well to add tables). Now reason for generating scaffold for Organiztaions only is because you would not need forms (views) for PhoneNumbers or Addresses. But you would be relating addresses to some organization and phone_numbers to some address so that will be handled through code later and I will explain you to keep it simple and make sure its working. Now first you would need to run the following commands:
rails generate scaffold organizations org_name:string org_description:text webdomain:string
rails generate model address offc_addr:text organization_id:integer
rails generate model phone_number offc_ph:string address_id:integer
Now this will generate all you require. Next you would need to run the following command to add tables in database.
rake db:migrate
Once you are done with that you would need to tweak your models as mentioned below:
#FOR ORGANIZATIONS
class Organization < ActiveRecord::Base
attr_accessible :org_description, :org_name, :webdomain
has_many :addresses
#THIS IS TO MAKE IT EASIER TO MANAGE IT IN VIEWS (read about nested attributes if you do not know)
accepts_nested_attributes_for :addresses
end
#FOR ADDRESS
class Address < ActiveRecord::Base
attr_accessible :offc_addr, :organization_id
belongs_to :organization
has_many :phone_numbers
#THIS IS TO MAKE IT EASIER TO MANAGE IT IN VIEWS (read about nested attributes if you do not know)
accepts_nested_attributes_for :phone_numbers
end
#FOR PHONE NUMBER
class PhoneNumber < ActiveRecord::Base
attr_accessible :address_id, :offc_ph
belongs_to :address
end
Thats all your required to do. Now just generate fields_for addresses using organization object and fields_for phone_numbers using address object in your views. For your help you can read nested_attributes behaviour from the link below:
http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
I hope this will help you Wasi and will clear things for you. Cheers :)
Upvotes: 2
Reputation: 17631
Using
rails generate migration
Will only create a migration file, no view or controller. This is a good way to add new tables or new fields to your db schema.
Upvotes: 0
Reputation: 845
I think you can create three table for it, org, address and phone. Then make relationship with them.
First table is the table you have created, remove the address and phone line. org_name, org_description, webdomain
Second table is address. offc_addr, org_id
Third table is phone address. offc_phone, org_phone_id
Then change the model. Org has_many offc_addr and offc_addr has_many offc_phone
Upvotes: 0
Reputation: 3467
You need a model:
rails generate model address organization_id:integer, street:string zip:string city:string //etc
This will generate an address.rb
in the models folder and a database migration.
Then migrate the database:
rails db:migrate
and you'll have the table in the database.
Then you'll need to look into belongs_to
and has_many
etc for tying them together in your code.
Upvotes: 0