I want to run rails g scaffold command to generate my billing plans table.
I want it like this:
id: string
name: string
description: string
status: enum["enabled", "disabled", "coming_soon"]
price: integer
How can I use that generator to accomplish this? I don't know what to put here rails g scaffold BillingPlans name:string description: string price:integer status: <My awesome enum type>
Using:
Rails 7
Postgresql
To straight up answer your question:
rails g scaffold BillingPlans name:string description: string price:integer status:integer
The enum would be a normal integer field combined with adding enum status: [:enabled, :disabled, :coming_soon] in the BillingPlans model. (Really nice blogpost about this -> https://naturaily.com/blog/ruby-on-rails-enum)
Now how I would personally do this, don't use scaffold. It creates so many files, just simply create a migration and write everything manually. You'll have full control of every line of code this way.
Second, I honestly don't know what the upside is of having an enum field instead of a normal string that is validated to be one of the 3 values you mentioned...
Related
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.
Context
When I generate the fixture file, rails generate model Post title:string text:text it shows:
one:
title: MyString
text: MyText
two:
title: MyString
text: MyText
Question
I was wondering why are there 2, and why are they named "one" and "two".
Here I see that names like "google", "rubyonrails", and "parent/child" are used; whereas, following the tutorial for generating the posts model, it generates just one and two...
Upon more research, I found that I might also be interested in db/migrate files. My current theory is that these files create the structure of my data... so I'm not so sure what fixtures does.
Reason
I'm trying to create a "Student" model using
rails generate scaffold student
but it doesn't seem to have :name as one of its keys. I'm looking into fixtures to add data columns.
Just some quick notes on your question:
Fixtures are data that you can feed into your unit testing. They are automatically created whenever rails generates the corresponding tests for your controllers and models. They are only used for your tests and cannot actually be accessed when running the application.
By default, Rails provides two fixtures named 'one' and 'two' every time. You can change them as you so please, Also, the data that goes into the fixtures is made when you pass in the keys for the database columns you want when using the generator. In the the first example were you used rails g model Post title:string... you created a model called Post and passed in two keys: :title and :text.
Answer:
As for your last question, you can quickly resolve the issue by
a) Deleting the old scaffold by typing the following in your command line:
rails d scaffold Student
b) Creating it again but this time with the keys you want:
rails g scaffold Student name:string
I'll start by saying that the code generated by the rails generate command is intended to be a starting point for parts of your application, helping you get going quickly.
That said - fixtures are intended for use in unit tests. They give you a way to generate a set of objects already existing in the system, so you don't have to create them by hand at the beginning of your test.
In this case, one and two are just placeholders. In a real app, you'd replace them with names that were more meaningful in your tests.
If you're looking to add columns of data in your app, fixtures probably aren't the right approach. They're really meant for use in tests, nothing more.
Try this:
As for your last question, you can quickly resolve the issue by a) Deleting the old scaffold by typing the following in your command line:
rails d scaffold Student
b) Creating it again but this time with the keys you want:
rails g scaffold Student name:string
Here's my very small, basic test website use case I'm building:
A 'Posting' belongs to a 'User' and a 'User' can have many 'Posting's.
I know that in the database there has to be a foreign key relationship. Do I need to specify this foreign key in the generator?
rails generate Posting name:string body:text post_date:date User:User???
Thank you for your suggestions.
Just make it an integer with model name underscore id
rails g Post user_id:integer
rails g Posting name:string body:text post_date:date user:references
By the way, if your field type is string, you don't actually need to specify it's type - rails will set it to string by default.
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.
rails g migration add_anonymous_to_message anonymous:???
If I were adding a title etc to a message then I would put rails g migration add_title_to_message title:string but if :anonymous is a check box in the message submit form how do I add it to the database so that there are only two options: box checked=anonymous and box unchecked=username displayed?
Thanks
Are you sure you need another database column? What you could do is
Add a column username:string which allows NULL values (default)
Validate the model so that if anonymous is unchecked, saving a blank username is invalid
Validate the model so that if anonymous is checked, the username is always saved as blank (nil), regardless of the form value
When you check later on whether or not a message is anonymous you simply check for message.username.nil?
If for some reason you do need a separate DB column for anonymous, it should look like this:
rails g migration add_anonymous_to_message anonymous:boolean
Although not all RDBMS support boolean columns (MySQL doesn't), Rails takes care of this by generating a TINYINT(1) or similar column when you specify boolean, which is set to either 0 or 1.
You have to use boolean type to store anonymous and string to store username:
rails g migration add_anonymous_and_username_to_message anonymous:boolean :default => false username:string