I have Authors model, which has
first_name
last_name
full_name
I need all three because when someone searches for an author, they need to search through the full names, but when I sort them, they need to be sorted by the last name and I can't just separate them on space, because some authors might have more than two names.
So, in the form where a user creates a new author, they have two entry fields - first_name and last_name. Since adding a third field for full_name is simply bad, and putting a hidden field that combines the value of first/last names is almost as bad, I was wondering how can I have only two fields, but on save combine their values and save that to the full_name column, without having an extra field, hidden or not?
authors_controller.rb
class AuthorsController < ApplicationController
def index
@authors = Author.order(:last_name)
respond_to do |format|
format.html
format.json { render json: @authors.where("full_name like ?", "%#{params[:q]}%") }
end
end
def show
@author = Author.find(params[:id])
end
def new
@author = Author.new
end
def create
@author = Author.new(params[:author])
if @author.save
redirect_to @author, notice: "Successfully created author."
else
render :new
end
end
end