STI with user and artist? Your thoughts please

I’ve got user and artist. The app allows users to register. And some
of these registered users can sign up as an artist as well.

Should user be an STI? Perhaps i can have user as an STI, member
(registered users) and artist models that inherit from user?

My concern is, if we go ahead with the STI approach. Consider this
scenario. A user registers. The record is saved and type=user. Later
on the same user signs up as an artist. How would we change the type
for this user? Should this be done in the Artist controller, create
method? i.e. a look up will be performed, and if user found (user.type
= ‘Artist’; user.save!)

Or should user and artist be separate tables on their own? i.e. user
has_one :artist, artist belongs_to :user?

Any other better approach?

I would go with having a boolean field for artist and then conditional
validarions, that is , if the field is true then have validations for
artist
apply along with the display of those extra field in the view, if the
user
stops been an artist, then it would stop validating and presenting those
field, in fact would add a validation that would forbid those fields
from
changing while the artist value is false.

Radhames, you are suggesting one table called user? and bolean fields
for is_artist, is_member? Is that right?

I think that the whether to separate or not Artists and Users for two
different tables depends
on whether are the models petty similar or not. If the only difference
is couple of new fields in the Artist
model and few new methods then there is no need to separate the
mapping into two tables. If there are
a lot of non-common columns you may also try to use :polymorphic =>
true association.

On Sep 30, 11:51 pm, Christian F. [email protected]

in case you wan to go with STI, create a controller to change type, and
access the attribute like this user[:type], not user.type

Yes, if the member will become an artist but and artist still has all
the
attributes of a member, is better to have one model with conditional
validation, something like.

validates :master_piece, :presence => true, :if => is_artist?

def is_artist?
artist
end

and in the view i would put some

<%= @member.master_piece if member.is_artist?%>

all these IF artist are member. I would use STI if member are not artist
but
share a few fields like name and so, but if an artist has all the fields
a
member has and a few more, there is no need for STI.

On Oct 1, 7:09 am, radhames brito [email protected] wrote:

Yes, if the member will become an artist but and artist still has all the
attributes of a member, is better to have one model with conditional
validation, something like.

validates :master_piece, :presence => true, :if => is_artist?

def is_artist?
artist
end

As an aside, there’s an even nicer syntax:

validates :master_piece, :presence => ture, :if => artist?

If “artist” is the name of a boolean column, ActiveRecord provides
“artist?” automatically. (Also, is_? style is more prevalent in
Java/.NET, but not in Ruby).

Jeff

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs