Topic: changing the type attribute

I read on "The Rails Way" that changing your type attribute is bad. I then read on here that it is ok to change your type attribute in some circumstances. I have 2 questions:

I have a users table with 3 types of users. I need to be able to change the "type" sometimes from one to another. One user can only have one type. Is STI ok for this?


If it is ok for this, my other problem is that when I try to update my attributes it validates against a the original user type, rather than the one i am changing to. Each user type has slight variances in required attributes so it is important to be able to validate against the correct class. any suggestions?

Travis

"They say, Evil prevails when good men do nothing. What they should have said was: Evil Prevails."

http://www.vrazzle.com

Re: changing the type attribute

STI is definitely the way to go when you have users that can be 1 of N number of types.

Could you post how these Users are being utilized in the system and why changing the 'type' column manually seems to be the best solution in your situation?

Also I have to warn you that I read the same article and I took them at their word and stopped changing the 'type' column and used the solution they provided, and I'm glad I did. (for reference for others here is the rails way article)

I imagine the old validations are getting triggered because you are setting the type column, changing attributes, and then calling model_obj.save, thus triggering the old validations.  I would try changing the type column, then call model_obj.save_with_validation(false) to skip the old validations, then call model_obj.save again to check it with the new validations.  It may not be the most efficient but I believe it will work.

"An egoless acceptance of stuckness is a key to an understanding of all Quality, in mechanical work as in other endeavors."
Zen and the Art of Motorcycle Maintenance

Re: changing the type attribute

Well the way the users are being used in the system first. I have 4 user types. Admin, Manager, Respondent, and Potential Respondent.

Admin can change an users information including other admins + has manager priveleges
Managers can change their own info plus info of any respondent. Can upgrade Potential Respondents to Respondents and can create new Respondents.

Both Potential Respondents and Respondents can currently only edit their own information. Eventually Respondents will have more priveleges though.

The main differences between a Potential Respondent and a Respondent are in data currently. Potential Respondents need only to have email, name, zip, and password. Respondents must also include gender, ethnicity, address, phone number, health questions, among other things.

A respondent created by an admin doesn't have to have an email address, but a potential respondent created by signing up on the website must have an email address. respondents with no email address's cannot log in and change their info (so I guess they aren't really users, but they use the site to be petitioned for surveys regardless of if the log in or not so they kind-of are users).



The problem with saving the type first without validation then saving with all the data is that I don't want potential respondents to become respondents before I know for sure that I have all the information I need from them. We are a research group and we must have as much data as possible from our respondents. Thanks for your help.

Travis Black

"They say, Evil prevails when good men do nothing. What they should have said was: Evil Prevails."

http://www.vrazzle.com

Re: changing the type attribute

hmm, I still can't judge if this would be an appropriate situation for type column changing.  But, funny enough, today while working on another project I came across a situation where changing the type column seemed appropriate.  I have contact information for users, but there are three different types of contact information.  Each type just has different validations, but are all from the same table using STI.  And the contact information type needs to be able to be changed at any time without losing data.  So I went the dirty route and did the type column changing.  For furture notice, if you end up changing the type column, the easiest way to do it without getting a bunch of nasty messages is:

@an_instance_of_a_model[:type] = 'AnotherExistingClass'

In response to your problem with saving twice:  try using rails transactions.  Just wrap both save statements in a transaction so that way if it fails, your model is still the old class and not the new one.  And for your second save, make sure to use save!, that way it raises an exception to fail the transaction if needed (I should note however that I have never tried to do this myself, I'm basing this advice on some quick api pursuing and some googling).

"An egoless acceptance of stuckness is a key to an understanding of all Quality, in mechanical work as in other endeavors."
Zen and the Art of Motorcycle Maintenance

Re: changing the type attribute

Ok because I am a Rails n00b I forgot all about trasactions. Transactions are amazing. Seriously. That is exactly what I needed.

Travis.

Thanks alot schickm

"They say, Evil prevails when good men do nothing. What they should have said was: Evil Prevails."

http://www.vrazzle.com