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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Christian Fazzini
2010-Oct-01 06:07 UTC
Re: STI with user and artist? Your thoughts please
Radhames, you are suggesting one table called user? and bolean fields for is_artist, is_member? Is that right? On Oct 1, 5:19 am, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Fazzini <christian.fazz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Oct-01 12:09 UTC
Re: Re: STI with user and artist? Your thoughts please
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Oct-01 12:11 UTC
Re: Re: STI with user and artist? Your thoughts please
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Oct 1, 7:09 am, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.