I have first_name and last_name fields currently represented in my model in rails. I’d like to add another field to my database called full_name and was wondering what the best way to do this seamlessly would be. Mind you I also have a production server going and would like to make it so that I not only add a column in my migration, but also can populate the new field with the existing data.
EDIT:
Here’s my controller
def followers
@title = "Followers"
@user = User.find(params[:id])
@users = @user.followers.paginate(page: params[:page])
@followers = @user.followers.find(:all, :select => 'users.id, users.first_name, users.last_name', :conditions => ["users.first_name like ?","%" + params[:q] + "%"])
respond_to do |format|
format.html {render 'show_follow'}
format.json {render :json => @followers}
end
end
I want to be able to 1. select: 'users.id, users.full_name and 2. :condition =>["users.full_name like ?", ...] and the only way I can think to do this is to modify the model. I also only want to return the properties id and full_name in the json object.
You will probably be better off just defining a full_name method in your model:
You can search by full name with something like:
Then define your own #to_json