Ruby on Rails Tuesday, July 11, 2017

On Tue, Jul 11, 2017 at 6:33 AM, Ralph Shnelvar <ralphs@dos32.com> wrote:

> I want to make sure the first user gets to set it's role attribute to enum
> :admin as the default when the record is created and all other users are
> enum :user.
>
> Of course a billion users is ridiculous. I'm just trying to understand what
> the, uh, best way to see if a Postgres table is empty without actually
> having to load it.

Assuming the table is "users" and User is an ActiveRecord model,
(User.count == 0)
should work.

However, doing that for every subsequent user creation seems like
a bit of a waste. I would just use the db/seeds.rb file to create that
first admin user and be done with it :-)

FWIW,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yArZbpBoLiCkeFkrbHjSA8ON%3D%3DX7pwqgnTRZ%3Dsz69gZMA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment