Jack
Jack

Reputation: 497

Rails Arrays and Mysql

I have a User with one or more email addresses, and I don't want to create a new table for emails. The idea was to store email addresses as Array, but how do I implement this in the controller and the model? This is the controller

class UsersController < ApplicationController
def update
@user =User.find(params[:id])

if @user.update_attributes(user_params)
  redirect_to :action => 'show', :id => @user

Is it possible to check during update if the email parameter is already there or a new one and if it is new add it to an Array? I don't know if I made myself clear, but thanks anyway for any answer you or suggestion you will provide! By the way, I am using mysql and I can't switch to a different one!

Upvotes: 3

Views: 5191

Answers (1)

Ravi Sankar Raju
Ravi Sankar Raju

Reputation: 2950

you can store it as a comma separated list.

or use rails serialize to do the job

For example,

class User < ActiveRecord::Base 
    serialize :emails, Array 
end

would maintain the array format when retrieved as user.emails. you can always validate whether an email is present in an array.

But my suggestion is to have a separate model. Future might hold new features like assigning primary and secondary emails and what not. then this design would not be so helpful

Upvotes: 7

Related Questions