Rails with Devise: Set Unique Username from First Name - ruby-on-rails-3

In my app I'm trying to automatically populate a username column from the first_name field that I already have tied in to my Devise login system. In theory, it should just be the user's first_name if they are the only one with that name, but it should be something like "Mal the 4th" or "Jayne the 3rd" if there are other users with that first name already.
So far, in googling and consulting other SO posts (like this one) I have this basic structure in my registrations_controller:
before_create :set_username
private
def set_username
#users = User.where(first_name == self.first_name)
same_first_name_array = []
#users.each do |u|
same_first_name_array << u.first_name
end
if same_first_name_array.size = 0
self.username = first_name
else
self.username = first_name + " the " + ordinalize(same_first_name_array.size + 1)
end
end
But I'm struggling to fill in the blanks. So far it looks like the best way to do it is to do an if statement that checks if that first_name is unique and, if it's not to ordinalize some kind of count, but please let me know if there's a better or "more Ruby" way. Any help getting this to work would be appreciated!

I think your way is correct, however can be optmized definately, you can avoid the step in which you create a array and populate all the same names.
Also, try to see if you can create a index on the first name column, this will optimise the query.
self.username already points to name user has entered, you need to change it only if it has multiple occurrences so no need of if / else. change it only if there are multiple occurrence.
You can rewrite it like below
before_create :set_username
private
def set_username
#users = User.where(first_name == self.first_name)
self.username = #users.count.eql?(0) ? self.first_name : first_name + " the " + ordinalize(#users.count + 1)
end
Note: having user names likes this will create performance issues when your application scales and has large number of users.

Related

How to insert custom value after validation in rails model

This has been really difficult to find information on. The crux of it all is that I've got a Rails 3.2 app that accesses a MySQL database table with a column of type POINT. Without non-native code, rails doesn't know how to interpret this, which is fine because I only use it in internal DB queries.
The problem, however, is that it gets cast as an integer, and forced to null if blank. MySQL doesn't allow null for this field because there's an index on it, and integers are invalid, so this effectively means that I can't create new records through rails.
I've been searching for a way to change the value just before insertion into the db, but I'm just not up enough on my rails lit to pull it off. So far I've tried the following:
...
after_validation :set_geopoint_blank
def set_geopoint_blank
raw_write_attribute(:geopoint, '') if geopoint.blank?
#this results in NULL value in INSERT statement
end
---------------------------
#thing_controller.rb
...
def create
#thing = Thing.new
#thing.geopoint = 'GeomFromText("POINT(' + lat + ' ' + lng + ')")'
#thing.save
# This also results in NULL and an error
end
---------------------------
#thing_controller.rb
...
def create
#thing = Thing.new
#thing.geopoint = '1'
#thing.save
# This results in `1` being inserted, but fails because that's invalid spatial data.
end
To me, the ideal would be to be able to force rails to put the string 'GeomFromText(...)' into the insert statement that it creates, but I don't know how to do that.
Awaiting the thoughts and opinions of the all-knowing community....
Ok, I ended up using the first link in steve klein's comment to just insert raw sql. Here's what my code looks like in the end:
def create
# Create a Thing instance and assign it the POSTed values
#thing = Thing.new
#thing.assign_attributes(params[:thing], :as => :admin)
# Check to see if all the passed values are valid
if #thing.valid?
# If so, start a DB transaction
ActiveRecord::Base.transaction do
# Insert the minimum data, plus the geopoint
sql = 'INSERT INTO `things`
(`thing_name`,`thing_location`,`geopoint`)
values (
"tmp_insert",
"tmp_location",
GeomFromText("POINT(' + params[:thing][:lat].to_f.to_s + ' ' + params[:thing][:lng].to_f.to_s + ')")
)'
id = ActiveRecord::Base.connection.insert(sql)
# Then load in the newly-created Thing instance and update it's values with the passed values
#real_thing = Thing.find(id)
#real_thing.update_attributes(b, :as => :admin)
end
# Notify the user of success
flash[:message] = { :header => 'Thing successfully created!' }
redirect_to edit_admin_thing_path(#real_thing)
else
# If passed values not valid, alert and re-render form
flash[:error] = { :header => 'Oops! You\'ve got some errors:', :body => #thing.errors.full_messages.join("</p><p>").html_safe }
render 'admin/things/new'
end
end
Not beautiful, but it works.

How to Write Where Clause Condition in Rails using AND & OR Operator

def self.get_previous_feedback current_feedback
Feedback.where("feedbacks.id < ?", current_feedback.id).order('created_at asc').last
end
def self.get_next_feedback current_feedback
Feedback.where("feedbacks.id > ?", current_feedback.id).order('created_at asc').first
end
#current_feeedback is the show page of any feedback.( feedback/show/id=2)
I have got 3 tables in my DB. Feedback, User, Department are connected in one-many relation.
By running above codes I am able to navigate to next/previous Feedback.
My User (current_user) is logged in, and Now on clicking prev/next, I want to retrieve the next feedback from DB(where condition written above) + whose feedback.department_id = current_user.deparment_id.
For including department_id in need to write an AND statement. How to do that ?
Try this...
def self.get_previous_feedback(current_feedback,current_user)
Feedback.where("id < ? & department_id = ?", current_feedback.id, current_user.department_id).order('created_at asc').last
end
def self.get_next_feedback(current_feedback,current_user)
Feedback.where("id > ? & department_id = ?", current_feedback.id, current_user.department_id).order('created_at asc').first
end
Thanks for pointing me out the correct logic.
Above query needs bit modification to work correctly.
simply id does not link to the feedbacks table, hence had to use feedbacks.id
& needs to be replaced by AND.
Corrected Code :
Feedback.where("feedbacks.id < ? AND feedbacks.department_id = ?",
current_feedback.id, current_user.department_id).order('created_at
asc').last
Thanks Man ! :)
You can also chain the two conditions, that way you don't need use the AND and you can also re-use the department_id clause.
def self.get_department_feedback current_user
Feedback.where(department_id: current_user.deparment_id)
end
def self.get_previous_feedback(current_feedback,current_user)
get_deparment_feedback(current_user).where("feedbacks.id < ?", current_feedback.id).order('created_at asc').last
end
def self.get_next_feedback(current_feedback,current_user)
get_deparment_feedback(current_user).where("feedbacks.id > ?", current_feedback.id).order('created_at asc').first
end

Rails Active Record Search - Name includes a word

Im trying to pull all records from a Project model that includes in the project_name the word 'Fox'. I can do an active record search and return specific project_names, like 'Brown Fox':
#projects = Project.where("project_name like ?", "Brown Fox")
But if I want to return all the names that INCLUDE 'Fox', this does not work unless the complete project name is 'Fox':
#projects = Project.where("project_name like ?", "Fox")
How do I do a search that returns all the objects with the word 'Fox' in the name?
Try using:
variable = "Fox"
Project.where("project_name like ?", "%#{variable}%")
You can use the SQL % operator:
#projects = Project.where("project_name like ?", "%Fox%")
Note that if you want your query to return results ignoring the word case, you can use PostgreSQL ilike instead of like.
Did you try ransack ?
With ransack you can do something like
#projects = Project.search(:project_name_cont => "Fox")
If you think it is too much for what you need. you can use the % operator as MurifoX said
Here's a version that will allow you to handle any number of input words and to search for all of them within a name. I was looking for this answer and didn't find the more complicated case, so here it is:
def self.search(pattern)
if pattern.blank? # blank? covers both nil and empty string
all
else
search_functions = []
search_terms = pattern.split(' ').map{|word| "%#{word.downcase}%"}
search_terms.length.times do |i|
search_functions << 'LOWER(project_name) LIKE ?'
end
like_patterns = search_functions.join(' and ')
where("#{like_patterns}", *search_terms)
end
end

Best way to combine first_name and last_name columns in Ruby on Rails 3?

I have a method in my User model:
def self.search(search)
where('last_name LIKE ?', "%#{search}%")
end
However, it would be nice for my users to be able to search for both first_name and last_name within the same query.
I was thinking to create a virtual attribute like this:
def full_name
[first_name, last_name].join(' ')
end
But is this efficient on a database level. Or is there a faster way to retrieve search results?
Thanks for any help.
Virtual attribute from your example is just class method and cannot be used by find-like ActiveRecord methods to query database.
Easiest way to retrive search result is modifying Search method:
def self.search(search)
q = "%#{query}%"
where("first_name + ' ' + last_name LIKE ? OR last_name + ' ' + first_name LIKE ?", [q, q])
end
where varchar concatenation syntax is compatible with your database of choice (MS SQL in my example).
The search functionality, in your example, is still going to run at the SQL level.
So, to follow your example, your search code might be:
def self.search_full_name(query)
q = "%#{query}%"
where('last_name LIKE ? OR first_name LIKE ?', [q, q])
end
NOTE -- these sorts of LIKE queries, because they have a wildcard at the prefix, will be slow on large sets of data, even if they are indexed.
One way this can be implemented is by tokenizing (splitting) the search query and creating one where condition per each token:
def self.search(query)
conds = []
params = {}
query.split.each_with_index do |token, index|
conds.push "first_name LIKE :t#{index} OR last_name LIKE :t#{index}"
params[:"t#{index}"] = "%#{token}%"
end
where(conds.join(" OR "), params)
end
Also make sure you prevent SQL injection attacks.
However, it's better to use full-text searching tools, such as ElasticSearch and its Ruby gem named Tire to handle searches.
EDIT: Fixed the code.
A scope can be made to handle complex modes, here's an example from one project I'm working on:
scope :search_by_name, lambda { |q|
if q
case q
when /^(.+),\s*(.*?)$/
where(["(last_name LIKE ? or maiden_name LIKE ?) AND (first_name LIKE ? OR common_name LIKE ? OR middle_name LIKE ?)",
"%#{$1}%","%#{$1}%","%#{$2}%","%#{$2}%","%#{$2}%"
])
when /^(.+)\s+(.*?)$/
where(["(last_name LIKE ? or maiden_name LIKE ?) AND (first_name LIKE ? OR common_name LIKE ? OR middle_name LIKE ?)",
"%#{$2}%","%#{$2}%","%#{$1}%","%#{$1}%","%#{$1}%"
])
else
where(["(last_name LIKE ? or maiden_name LIKE ? OR first_name LIKE ? OR common_name LIKE ? OR middle_name LIKE ?)",
"%#{q}%","%#{q}%","%#{q}%","%#{q}%","%#{q}%"
])
end
else
{}
end
}
As you can see, I do a regex match to detect different patterns an build different searches depending on what is provided. As an added bonus, if nothing is provided, it returns an empty hash which effectively is where(true) and returns all results.
As mentioned elsewhere, the db cannot index the columns when a wildcard is used on both sides like %foo%, so this could potentially get slow on very large datasets.

Advanced search on multiple associations in Rails

Im trying to build advanced search finder for my Candidate model.
Lets imagine it has couple fields + multiple associations like has_many: languages & has_many: skills. Now I'm building query like this:
query = Candidate.select("*")
if position_name
query = query.where('position_name LIKE ? OR position_name IS NULL',"%#{position_name}%")
end
if salary
query = query.where('salary <= ? OR salary IS NULL',salary)
end
and so on...
Now I want to add more advanced conditions like to find users who only have such skills like PHP and Java (so return only those users who have both skills)
This works but only when I insert OR
query = query.joins(:skills)
query = query.where('`skills`.`name` = ? OR `skills`.`name` = ?',"Java","PHP")
Additionally I'd like the same also for languages (plus, language have language.name & language.level)
Can someone points me in which direction to look? And also how to build such condition where I can multiple skills or multiple languages?
Have a look at the various search gems like Ransack, Metawhere or Searchlogic
http://rubygems.org/gems/ransack
https://github.com/railsdog/searchlogic
Both Ransack and Searchlogic allow searching on associated models and you can use scopes to restrict the search parameters.
Example Search params for Searchlogic.
[search][admitted_gte]
[search][admitted_lte]
[search][aetiology_like_any][] VIRUS
[search][at_risk_gte]
[search][at_risk_lte]
[search][died_gte]
[search][died_lte]
[search][gezi_reference_like]
[search][id]
[search][incidents_location_encrypted_postcode_like]
[search][lab_confirmed_gte]
[search][lab_confirmed_lte]
[search][onset_first_after]
[search][onset_first_before]
[search][onset_last_after]
[search][onset_last_before]
[search][outbreak_type_equals_any][] FOODBORNE
[search][point_source_date_after]
[search][point_source_date_before]
[search][total_affected_gte]
[search][total_affected_lte]
[search][user_reference_like]
[search][year_equals_any][] 2010
search[order] descend_by_id
Outbreak_Controller.rb Index action returns the results of the Search query. From 17 Search params only a single searchlogic call is required #search = Outbreak.search(params[:search]). The params are whitelisted against a list of allowed search params - code not shown.
def index
#set the default index order to be descending Outbreak id
if !params[:search][:order]
params[:search][:order] = "descend_by_id"
end
if params[:search][:bacterial_agents_bacterium_name_like_any] != nil && !params[:search][:bacterial_agents_bacterium_name_like_any].empty?
params[:search][:bacterial_agents_category_like] = "CAUSATIVE"
end
if params[:search][:viral_agents_virus_name_like_any] != nil && !params[:search][:viral_agents_virus_name_like_any].empty?
params[:search][:viral_agents_category_like] = "CAUSATIVE"
end
if params[:search][:protozoal_agents_protozoa_name_like_any] != nil && !params[:search][:protozoal_agents_protozoa_name_like_any].empty?
params[:search][:protozoal_agents_category_like] = "CAUSATIVE"
end
if params[:search][:toxic_agents_toxin_name_like_any] != nil && !params[:search][:toxic_agents_toxin_name_like_any].empty?
params[:search][:toxic_agents_category_like] = "CAUSATIVE"
end
#Outbreak.search takes all of the given params and runs it against the Outbreak model and it's associated models
#search = Outbreak.search(params[:search])
end