I have this line of code to find the row in database or else create a new row. Is working but it look very ugly and hard to maintain.
return_policy_id= MyEbayReturnPolicy.find_or_create_by_active_and_name_and_ebay_marketplace_id_and_returns_within_and_returns_accepted_and_warranty_offered_and_warranty_duration_and_warranty_type_and_shipping_costs_paid_by_and_refunds(active,name,ebay_marketplace_id,returns_within,returns_accepted,warrenty_offered,warranty_duration,warranty_type,shipping_costs_paid_by,refunds)
Here is the find_or_create code
def self.find_or_create(search, *args, &block)
parameters = search.split("_and_")
params = Hash[ parameters.zip(args) ]
obj = where(params).first
if(obj.nil?)
obj = self.new(params);
obj.save
end
return obj;
end
As you can see is extremely long line of code.
I wanted to know if there’s a better approach to make this code look nice and clean so easy for maintain.
Thanks!
Rails 3.2 introduced first_or_create (among other similar methods). You can take advantage of it: