所以我想要變得聰明,做一個通用的方法,可以創建變量名稱並正確使用它們。我將2個符號傳入此方法,並創建正確的變量並嘗試使用它們。紅寶石字符串插值和動態編程
def are_numeric_fields_valid (column_name)
single_name = column_name[0].to_s
aggregate_name = column_name[1].to_s
short_column_attribute = single_name.sub("_single", "")
effective_on_attribute = :"#{short_column_attribute}_effective_on"
expire_on_attribute = :"#{short_column_attribute}_expire_on"
if ("loa_item.#{effective_on_attribute}".present? && "loa_item.#{expire_on_attribute}".present?) && (no_values_present?(single_name, aggregate_name))
loa_item.errors.add(:base, "You must provide single and aggregate values when providing dates at the #{short_column_attribute} role level")
end
end
但是這始終是真實的,即使在實際值爲空:
if ("loa_item.#{effective_on_attribute}".present? && "loa_item.#{expire_on_attribute}".present?)
我需要別的東西所以這將正確評價?像發送?
我喜歡你喲* *看起來.. :) –
@ArupRakshit我很無聊瓦特/正常畫面;)! –