Note that there are some explanatory texts on larger screens.

plurals
  1. POIs subclassing a User model really bad to do in Rails?
    primarykey
    data
    text
    <p>I am getting lots of push back from Rails because I have subclassed User into many different subclasses. In my application, not all users are equal. There's actually a lot of model objects and not every User type has access to them.</p> <p>I also need a way to do polymorphic behaviour. For example, many methods will behave differently depending on the type. Isn't that what polymorphism is for?</p> <p>But the thing is, I am always getting push back from Rails. The defaults - especially the way forms submit to parameter hashes - seem to work like non-subclassed models. Links and parameter hashes are just two ways in which the defaults really bite you.</p> <p>What is the "right" way to handle complex logic for different types of Users in Rails? In Java, the subclassing model works - you don't have to go through hoops to get it to work the way you want to. But in Rails, it's hard to get subclasses to work with REST conventions, it punishes you when you forget to include <code>:as =&gt; :user</code>, or it punishes you when you put a subclassed object in links, such as <code>edit_user_path(@user)</code> &lt;- bad idea!</p> <p>There's one other area which is also very hard to deal with. Let's say I have a <code>Company</code> model and it has many <code>Users</code>. These users can be directors, instructors, trainees, etc - all different subclasses.</p> <p>When we create the account, we might want to use <code>accepts_nested_attributes_for :users</code>. However, if we use this, we can't specify the classes that it creates. Bloody hell!</p> <p>It seems like everything in Rails is designed to not want you to subclass your models. If you don't subclass, everything 'just works'. But if you subclass, you are in for hell.</p> <p>What is the solution?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload