So I'm trying to be smart and make a generic method which can create the variable names and use them correctly. I am passing into 2 symbols into this method, and creating the correct variables and trying to use them.
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
But this always is true, even when the actual values are blank:
if ("loa_item.#{effective_on_attribute}".present? && "loa_item.#{expire_on_attribute}".present?)
Do I need something else so this will evaluate correctly? Like a send?
Yes, it'll need to be different–you're calling present?
on a string that has a value: "loa_item.interpolated_value"
where interpolated_value
is the string representation of effective_on_attribute
's evaluation.
It can't not be present?
because there's an immediate, non-interpolated value.
You'd want something closer to:
loa_item.send(effective_on_attribute.to_sym) etc.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments