RahulOnRails
RahulOnRails

Reputation: 6542

ROR + Concatenate params with Prefix element in Controller before passing to any function

In my rails application, I am accessing one Param, after that I have to pass that param by concatenate with 3 different prefix to 3 different function. But I am not able to pass those values.

Sample: Params variable is params[:element] may be price or qty. If params[:element] is nil then default option is given with function call. But if price comes as parameter, then automatically filter_element has to product_price, item_price and package_price respectively. And if qty comes as parameter, then filter_element has to be product_qty, item_qty and package_qty.

def check_price

  #filter_element = ??? 

  # First Model - Prefix is Product
  first_price = current_user.first_model(filter_element ||= "product_price")

  # Second Model - Prefix is Item
  second_price = current_user.second_model(filter_element ||= "item_price")

  # Third Model - Prefix is Package
  third_price = current_user.third_model(filter_element ||= "package_price")
end

Here I am not able to concatenate params with prefix varialbles. Thanks in advance for any suggestion.

Upvotes: 0

Views: 95

Answers (1)

Amit Patel
Amit Patel

Reputation: 15985

I am still not sure about the requirement but I think this is what you want

def check_price
  # If nil then price otherwise can be either price or qty(assuming)
  suffix = params[:element] || 'price'

  # First Model - Prefix is Product
  first_price = current_user.first_model("product_#{suffix}")

  # Second Model - Prefix is Item
  second_price = current_user.second_model("item_#{suffix}")

  # Third Model - Prefix is Package
  third_price = current_user.third_model("package_#{suffix}")
end

Upvotes: 1

Related Questions