Rails generator model with optional validations and required?

10 views
Skip to first unread message

Emile Bosch

unread,
Aug 29, 2019, 5:55:17 AM8/29/19
to Ruby on Rails: Core
Hi there,

Like numerous times I am generating models, I go in to make fields required and validates_presence_of them.
Would it be easy if we could just say:

~~
rails g model contact *name *email:string *age:int
~~

Simple but super easy to add and makes it all a bit more dry.

Let me know what ya'll think and then I'll add it to rails <3

Hamad Al-Ghanim

unread,
Aug 29, 2019, 11:07:01 AM8/29/19
to Ruby on Rails: Core
I would rather have it look like the following

Rails g model contact name:string:required age:int:required

Emile Bosch

unread,
Aug 29, 2019, 11:13:03 AM8/29/19
to rubyonra...@googlegroups.com
Thanks for your participation!
Hmm, maybe. However I am afraid this might get a bit cumbersome when writing more then 4 fields. The word required in this sample is
the longest word of this command. Maybe req? Are there any other generators out there that tackle this problem?

Also an * (asterisk) is universal hint that something is required

> On 29 Aug 2019, at 17:07, Hamad Al-Ghanim <dreame...@gmail.com> wrote:
>
> I would rather have it look like the following
>
> Rails g model contact name:string:required age:int:required
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Core" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-core/PUGFir_OJ30/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to rubyonrails-co...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/bf680441-891b-4494-aa64-c5d8b518de34%40googlegroups.com.

James Coleman

unread,
Aug 29, 2019, 11:17:44 AM8/29/19
to rubyonra...@googlegroups.com
An asterisk is actually going to be very painful to use, because your
shell is going to treat it as a shell expansion by default rather than
passing it along to Rails.
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/AEB61F46-2823-4425-A4D3-480C90CCEAA4%4054hrs.com.

Emile Bosch

unread,
Aug 29, 2019, 11:30:38 AM8/29/19
to rubyonra...@googlegroups.com
Ha, well that makes the choice easy then, then :required is probably the way to go.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/CAAaqYe9CYwKN9PE4tV-%2BySbNrddYK7Wu2ZZmJUYkdCSd%3D6CVoA%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages