Small issue with user and artist model

Ok, Ive got the following models:User, Artist and Video

User is 1:1 with Artist (A user can register to be an artist)
Artist is 1:M with Video (An artist can have one or many videos)

Note that, before any user can register as an artist. He/she must
already have an account with the system. i.e. He must be a registered
user first. Hence the 1:1 relationship with User and Artist.

Here is the problem, when an artist tries to upload a video:

def create
@video = Video.new(params[:video])
@video.artist = current_user.artist


For this particular user:
In the User model, he has id => 29
In the Artist model, he has id => 1, user_id => 29 (user_id being the
foreign key)

This means, when the video record is saved to the db. It is saved as:
id => 1, artist_id => 1 (instead of 29), title => ‘foobar’, etc

How can such be fixed? Should I remodel my schema? I was thinking of
just having one User model and have this as an STI. Member (Registered
users) and Artist models would inherit from the User model.

What is a better approach to fixing this?

Christian F. wrote:

Ok, Ive got the following models:User, Artist and Video

User is 1:1 with Artist (A user can register to be an artist)
Artist is 1:M with Video (An artist can have one or many videos)

Note that, before any user can register as an artist. He/she must
already have an account with the system. i.e. He must be a registered
user first. Hence the 1:1 relationship with User and Artist.

Here is the problem, when an artist tries to upload a video:

def create
@video = Video.new(params[:video])
@video.artist = current_user.artist


For this particular user:
In the User model, he has id => 29
In the Artist model, he has id => 1, user_id => 29 (user_id being the
foreign key)

This means, when the video record is saved to the db. It is saved as:
id => 1, artist_id => 1 (instead of 29), title => ‘foobar’, etc

How can such be fixed?

There’s nothing to fix. artist_id is giving the ID of the artist
record, not the user record. This is exactly what you’ve asked Rails to
do.

Should I remodel my schema? I was thinking of
just having one User model and have this as an STI. Member (Registered
users) and Artist models would inherit from the User model.

That might work, or just have a role field in your User model, and only
role “artist” can upload videos.

Or get rid of the distinction altogether, and do something like
class User
def artist?
self.videos.size > 0
end
end

What is a better approach to fixing this?

I don’t think it’s broken.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hmmm, now that I think about it. Yes, you are right…

On the side. Marnen, would you keep this schema as is, or would you
modify it, in any way? Perhaps, as I was suggesting about having the
User model as an STI? Or even polymorphic? What are your thoughts?