Hi all,
I’ll do my best to explain this… I have the possibility to have two
customer types in my system:
- A “mailing list” type: new records are saved when user enters email
address in the mailing list subscription form. - A “full” type: new records are saved when the user buys something and
provides all their billing/shipping details, etc.
Model:
- I’d prefer to have 1 customers table with a column to mark their
status.
Validation:
- The customer email address is common to both so validation on this is
easy. - I want the “full” type to have extended validation rules, first and
last name for example. - I’d like to enforce a unique email address.
Questions:
-
Rails looks to support model based validation well. Is there a way
to specify validation rules depending on which customer type is being
saved (as I only want email validated for a mailing list type, but for
the full type I need validation on pretty much everything)? Should I
have 2 models? How do I map this to a single database table? -
How do I code an upgrade on a customer? For example, if the customer
is new and does not have an existing record, then when they buy
something and fill out their details there is no problem here (besides
the validation issue above). But, if the customer does have an existing
record, is there an elegant way to “merge” the extended details from the
form with the existing email address marked record on the database?
Currently I have the code:form = params[:new_customer] email = form[:email] tmp_customer = Customer.find_by_email(email) if tmp_customer != nil && (tmp_customer.level == 'mailinglist'
|| tmp_customer.level == ‘test’)
@customer = Customer.new(params[:new_customer])
@customer.id = tmp_customer.id
@customer.level = ‘full’
if @customer.update_attributes(@customer)
flash[:notice] = “Customer details upgraded
successfully.”
redirect_to(:action => ‘payment_details’)
else
render(:action => ‘checkout’)
end
else
@customer = Customer.new(params[:new_customer])
@customer.level = ‘full’
if @customer.save
flash[:notice] = “Customer details saved successfully.”
redirect_to(:action => ‘payment_details’)
else
render(:action => ‘checkout’)
end
end
But with this I get the error: undefined method `stringify_keys!’ for
#Customer:0x38b1ac0
My attempt was to get the existing customer, set the id of the form data
to the record on the database and then perform an update. I’m not sure
however if I have covered all of the Rails requirements to do something
like this. But, possibly (most likely due to my lack of Rails
experience), there is a better way.
Sorry for the long post, I hope it makes sense, can anyone help?
Thanks,
Dan