我有一个窗体可以处理同一窗体的四种不同类型的构面。在我的SQL专栏中,我有四个不同的属性。我如何重构一个简单的动态属性?
其中只有一个人将会有数据。
Distribution =>
zip_code: nil
me_topic: nil
sex: nil
age: nil
为了区分它们,我想成立一个case语句,和动态属性添加到创建呼叫:
@type = case params[:type]
when "zip" then ":zip_code"
when "interest" then ":me_topic"
when "sex" then ":sex"
when "age" then ":age"
end
@cur_item = Distribution.new(@type => params[:value])
# Unfortunately, this is not the proper way to create a dynamic attribute
@distribution = @email.distributions.create(params[:distributions])
什么是完成这一正确的语法声明?
哇,我爱你的风格。听起来很棒! – Trip 2011-05-11 00:36:04