mysql rails,Rails数组和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!

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值