如何重构一个简单的动态属性?

发布于 2024-10-24 01:50:11 字数 667 浏览 2 评论 0原文

我有一个表单可以处理同一表单的四种不同类型的方面。在我的 SQL 列中,我有四个不同的属性。

其中只有一个会有数据。

Distribution  =>
   zip_code: nil
   me_topic: nil
   sex: nil
   age: nil

为了区分它们,我想设置一个 case 语句,并向 create 调用添加一个 dynamic 属性:

@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])  

完成此语句的正确语法是什么?

I have a form that handles four different types of facets of the same form. In my SQL column, I have the four different attributes.

Only one of them is going to have data in it.

Distribution  =>
   zip_code: nil
   me_topic: nil
   sex: nil
   age: nil

In order to differentiate between them, I wanted to set up a case statement, and add a dynamic attribute to the create call :

@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])  

What is the proper syntax for completing this statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇女薄笑 2024-10-31 01:50:11

声明一个名为 type_map 的方法

def type_map params
  @@type_map ||= {
    "zip"      => :zip_code, 
    "interest" => :me_topic, 
    "sex"      => :sex, 
    "age"      => :age
  }
  { @@type_map[params[:type]] => params[:value]
end

现在您可以按如下方式使用地图:

@distribution = @email.distributions.create(type_map(params)) 

Declare a method called type_map

def type_map params
  @@type_map ||= {
    "zip"      => :zip_code, 
    "interest" => :me_topic, 
    "sex"      => :sex, 
    "age"      => :age
  }
  { @@type_map[params[:type]] => params[:value]
end

Now you can use the map as follows:

@distribution = @email.distributions.create(type_map(params)) 
瑕疵 2024-10-31 01:50:11

这就是我的想法,但请随意最好地回答我的问题。

  @cur_item = case params[:type]
    when "zip"      then {:zip_code => params[:value]}
    when "interest" then {:me_topic => params[:value]}
    when "sex"      then {:sex => params[:value]}
    when "age"      then {:age => params[:value]}
  end

  @distribution = @email.distributions.create(@cur_item)  

This is what I went with, but feel free to best my answer.

  @cur_item = case params[:type]
    when "zip"      then {:zip_code => params[:value]}
    when "interest" then {:me_topic => params[:value]}
    when "sex"      then {:sex => params[:value]}
    when "age"      then {:age => params[:value]}
  end

  @distribution = @email.distributions.create(@cur_item)  
爱你不解释 2024-10-31 01:50:11

好吧,改进代码(您在答案中发布的内容)的一种方法是排除重复的 params[:value] ,如下所示:

key = case params[:type]
  when "zip"      then :zip_code
  when "interest" then :me_topic 
  when "sex"      then :sex
  when "age"      then :age
end

@cur_item = { key => params[:value] }
@distribution = @email.distributions.create @cur_item

Well, one way to improve your code (what you posted in your answer) would be to factor out the repeated params[:value] as follows:

key = case params[:type]
  when "zip"      then :zip_code
  when "interest" then :me_topic 
  when "sex"      then :sex
  when "age"      then :age
end

@cur_item = { key => params[:value] }
@distribution = @email.distributions.create @cur_item
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文