英文:
Rails ActiveRecord update attribute from string
问题
我尝试使用.to_sym
,但没有成功。甚至不知道这是否可能。
我想根据一个字符串参数来更新活动记录的属性:
def update_settings(type, value)
value = ActiveModel::Type::Boolean.new.cast(value)
case type
when 'sync_spotify'
update(sync_spotify: value)
when 'allowed_use_google_token'
update(allowed_use_google_token: value)
when 'allowed_use_spotify_token'
update(allowed_use_spotify_token: value)
when 'locale'
update(locale: value)
end
end
type
参数将始终与数据库中的列名匹配。
有线索吗?这是否可能?
英文:
I tried to use the .to_sym
with no luck. Don't even know if this is possible.
I want to update an attribute from an active record based on a string parameter:
def update_settings(type, value)
value = ActiveModel::Type::Boolean.new.cast(value)
case type
when 'sync_spotify'
update(sync_spotify: value)
when 'allowed_use_google_token'
update(allowed_use_google_token: value)
when 'allowed_use_spotify_token'
update(allowed_use_spotify_token: value)
when 'locale'
update(locale: value)
end
end
The type
parameter will always match the column name in the database.
Any clue? Is this even possible?
答案1
得分: 2
你可以定义一些允许列表,以检查更新属性是否在此列表中。
然后进行动态更新。
类似于这样:
BOOLEANS_ALLOWED_FOR_UPDATE = %i[
sync_spotify
allowed_use_google_token
allowed_use_spotify_token
locale
].freeze
def update_settings(type, value)
type = type.to_sym
raise ArgumentError unless BOOLEANS_ALLOWED_FOR_UPDATE.include?(type)
value = ActiveModel::Type::Boolean.new.cast(value)
update(type => value)
end
当然,你可以引发一些自定义错误,或向模型实例添加错误,或者只是返回。但无论如何,在更新之前使用一些守卫条款是值得的。
英文:
You can define some allowed list to check if updating attribute is in this list
And then update dynamically
Something like this
BOOLEANS_ALLOWED_FOR_UPDATE = %i[
sync_spotify
allowed_use_google_token
allowed_use_spotify_token
locale
].freeze
def update_settings(type, value)
type = type.to_sym
raise ArgumentError unless BOOLEANS_ALLOWED_FOR_UPDATE.include?(type)
value = ActiveModel::Type::Boolean.new.cast(value)
update(type => value)
end
Of course you can raise some custom error, or add error to the model instance, or just return. But in any case it is worth using some guard clause before updating
答案2
得分: 0
Here is the translated code:
你可以尝试以下代码:
def update_settings(type, value)
value = ActiveModel::Type::Boolean.new.cast(value)
case type
when 'sync_spotify'
update(sync_spotify: value)
when 'allowed_use_google_token'
update(allowed_use_google_token: value)
when 'allowed_use_spotify_token'
update(allowed_use_spotify_token: value)
when 'locale'
update(locale: value)
else
# 如果type参数与任何列名都不匹配,抛出错误或以其他方式处理
raise ArgumentError.new("无效的type参数: #{type}")
end
end
在每种情况下,你可以用以下代码替换“update”方法调用:
send("#{type}=", value)
Please note that the translated code contains the same content as the original code, with the translated comments.
英文:
can you try following?
def update_settings(type, value)
value = ActiveModel::Type::Boolean.new.cast(value)
case type
when 'sync_spotify'
update(sync_spotify: value)
when 'allowed_use_google_token'
update(allowed_use_google_token: value)
when 'allowed_use_spotify_token'
update(allowed_use_spotify_token: value)
when 'locale'
update(locale: value)
else
# If the type parameter doesn't match any of the column names,
# raise an error or handle it in some other way
raise ArgumentError.new("Invalid type parameter: #{type}")
end
end
You can replace the update
method calls in each case with the following code:
send("#{type}=", value)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论