Locating existing records
Rails already has a built-in mechanism for doing this with the first_or_initialize method within ActiveRecord::Relation.

As an example, you can perform the following

Service.where(protocol: params[:protocol], port_from: params[:port_from], port_to: params[:port_to]).first_or_initialize


This will try and find a service with the given attributes OR initialize a new object with those attributes set if it can't find it.

Hope this helps.