Added customer composite key
This commit is contained in:
@@ -21,7 +21,7 @@ class CustomersController < ApplicationController
|
||||
# POST /customers or /customers.json
|
||||
def create
|
||||
@customer = Customer.new(customer_params)
|
||||
@customer.company = @company
|
||||
@customer.company = Company.first # Set the first company
|
||||
|
||||
respond_to do |format|
|
||||
if @customer.save
|
||||
@@ -57,15 +57,35 @@ class CustomersController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def search
|
||||
@customers = @company.customers.where(
|
||||
"LOWER(first_name) LIKE :query OR LOWER(surname) LIKE :query OR original_phone LIKE :query",
|
||||
query: "%#{params[:q].downcase}%"
|
||||
).limit(10)
|
||||
|
||||
render json: @customers.map { |c|
|
||||
{
|
||||
id: "#{c.first_name}_#{c.surname}_#{c.original_phone}",
|
||||
label: "#{c.full_name} (#{c.original_phone})",
|
||||
birthyear: c.birthyear
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_customer
|
||||
@customer = Customer.find(params[:id])
|
||||
first_name, surname, original_phone = params[:composite_key].split('_')
|
||||
@customer = Customer.find_by!(
|
||||
first_name: first_name,
|
||||
surname: surname,
|
||||
original_phone: original_phone
|
||||
)
|
||||
end
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
def customer_params
|
||||
params.require(:customer).permit(:name, :phone, :notes, :email, :birthyear)
|
||||
params.require(:customer).permit(:first_name, :surname, :phone, :notes, :email, :birthyear)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user