i want to generate a scaffold to an object which already has table in the database, so i don't need to migrate and stuff, but the question is, when i am generating a scaffold, for example:
rails generate scaffold Product title:string description:text image_url:string price:decimal
do i still to specify those fields ? (title, description, etc'..) or those fields are only used for the migration part, so if i already have a table, i don't need to specify them ..
thanks!
Generator is reading the fields you supplied and is building all the views (including forms) based on the input. It does not read the current db schema, so yes, you have to supply all the attributes.
UPDATE:
Note that it wills still work without attributes, but it will generate empty views.
Related
I creating an app that needs to be able to create and drop tables on the fly, on create it's pretty simple I use:
rails g model User name:string
but there is no generator for dropping a table.
I want to make it be possible to:
rails g migration DropTableUser
To correct your question, rails g migration User name:string would result in a migration called User not a model, I guess what you meant was rails g model User name:string.
To answer your question, I think it would result in an unnecessary mental effort, especially since something like this already exists in:
rails [d|destroy] model User
If you want to still implement yours, I'd advise that you look at Rails Generators instead for a way to help you out. I guess that would be dependent on creating a migration template etc.
Is there a way to specify the fields and relationships in the model and run a command to the rails create tables in the database based on the models, and then I can create or remove other fielda, change some relationships and run this command again and rails do update the tables in the database? I thought the migrate did it but after I read about it seems that it only creates the tables once and has no concept of model / db sync that i need.
No you need to create a new migration anytime you need to update the database. Changing some code in the model will not generate a migration.
No, but if you want a simpler way to write your migrations etc, I suggest you read more about the generators :)
For example :
rails generate model user first_name:string last_name:string birth_date:datetime friend:references
will directly generate the migration with the correct fields :)
And if you want to add a field after, you can write it like :
rails generate migration AddPseudoToUsers pseudo:string
And it will be correctly written normally :)
This is the doc
http://guides.rubyonrails.org/migrations.html#creating-a-migration
http://guides.rubyonrails.org/command_line.html#rails-generate
I want to create a model with many fields on rails. I want two of the ten fields to have two or three options to choose from to check off.
The guide I'm using has only two fields, as follows:
rails generate scaffold topic title:string description: text.
It seems like the easy way out is to do a migration in this format:
rails generate migration AddClosing_Hrs1ToBusinesses closing_hrs1:string new_cloumn:string third_column:string
(source: How to add several columns to a database in Rails).
Any advice?
It depends on where you are in the process.
If you have not yet created the model at all you can just add more fields to the original generate scaffold line. For example if you wanted 4 fields:
rails generate scaffold topic title:string description:text another_item:string some_number:integer
If you already created the model but have not yet run rake db:migrate then you can find the migration file in app_name/db/migrate/. Open the file and add lines for your new fields
If you have already run db:migrate then you should follow the answer for "How to add several columns to a database in Rails" that you linked in your answer.
For the fields where you want several options to select from those options will need to be handled by a combination of your model and view code. Assuming the options will stay constant and you can only select one first define the options array in your model using something like:
OPTIONS_FOR_TITLE = ["Title 1", "Title 2", "Title 3"]
Then in the view code you will use the select and options_for_select helpers to create the view. More details on them are available here:
http://guides.rubyonrails.org/form_helpers.html#the-select-and-option-tags
For lots of detailed information about migrations:
http://guides.rubyonrails.org/migrations.html
I used the following method to generate scaffold for platform list in our app:
rails generate scaffold platform name:string url:string country:string
I would like to still use scaffold possibility to add additional attributes, like type and gender, which should be drop downs, or preferably multi-select's.
Is there a way to use rails generate to create such a models?
You have already generated a scaffold for Platform. So you have two options:
edit scaffold by hand to add the new fields (recommended)
remove scaffold with rails destroy scaffold and then use rails generate again
Anyway scaffolding doesn't allow you to specify options for a drop-down menu. In any case you have to manually edit the scaffold. Furthermore in Rails migrations you cannot specify ENUM fields, so the best option for gender would be a string field with unitary length (with a validates_inclusion_of in model).
P.S. Don't use type as a column name because you will probably get strange behaviors.
I want to display a combobox in my web page.
I have two objects :
i.e.
rails generate scaffold person id:integer name:string
rails generate scaffold state id:integer name:string
I want on the person to be able to choose state.
What is the best practice?
should I run a different scaffold ?
(one that will create me a foreign key in the db and another code ?)
thanks.
If a Person can only be linked to a single State at any time, add a state_id field to Person. If a Person can have many States linked to it, for example, Residences you should create a object for that containing both the state_id and the person_id.
More on relations and linking models can be found in the Rails Guides
To render a dropdown list of states you can use the select helper method. Make sure to use the proper field name for the select input field(state_id), instead of the name of the association.(state)