Rails Ruby 基于参数值动态更新特定列的逻辑

Rails Ruby Logic for Dynamically updating specific Column Based on Param value

我的应用程序有来电显示功能,但我目前对如何根据 params[:call_number_type]

的值动态更新正确的列感到困惑

params[:call_number_type] 可以是 "alt_phone""cell_phone""office_phone"nil ... nil 默认为 "alt_phone" 将是理想的.

以上每个字符串都对应一个列名,如果 params[:call_number_type] 具有该值,我需要更新该列名。

@contact = Contact.find(params[:contact_id])

if @contact.update(this_needs_to_be_the_right_column_key: params[:call_number])

上面的更新语句必须根据 params[:call_number_type]

的值动态创建

如果有人能帮助我,那就太好了。谢谢。

试试这个

if @contact.update(call_number_type => params[:call_number])
  ...

private

def call_number_type
  params[:call_number_type].present? ? params[:call_number_type] : :alt_phone
end