Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7597869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:15:52+00:00 2026-05-30T22:15:52+00:00

I have kind of a tricky problem I’m working on in my current rails

  • 0

I have kind of a tricky problem I’m working on in my current rails app. In my app users share photos. Photos can be associated with a city, so City has_many :photos. I want users to be able to associate their photos with a city using autocomplete and a natural language syntax. Ie: New York, NY or Paris, France.

I’d like to do this with an autocomplete text box so that if the user types “Athens” they would see a list with:

Athens, Greece
Athens, GA

… and if the person actually wanted “Athens, TX” they could simply type that in and it would create a new city record.

My city model has the fields, name, state, country. State and Country are 2-letter postal codes (I use Carmen to validate them). I have a virtual attribute called full_name which returns “City, State Code” (like New York, NY) for North American cities, and “City, Country Name” (like Paris, France) for all others.

def full_name
    if north_american?
        [name, state].join(', ')
    else
        [name, Carmen.country_name( country )].join(', ')
    end
end

def north_american?
    ['US','CA'].include? country
end

My question is, to get the text field working, how can I create a find_or_create method that can accept a string with the city name and either state code or country name and find or create that record?


Update

Inspired by Kandada’s answer I came up with something a little different:

def self.find_or_create_by_location_string( string )
  city,second = string.split(',').map(&:strip)
  if second.length == 2
    country = self.country_for_state( second )
    self.find_or_create_by_name_and_state( city, second.upcase, :country => country )
  else
    country = Carmen.country_code(second)
    self.find_or_create_by_name_and_country( city, country )
  end
end

def self.country_for_state( state )
  if Carmen.state_codes('US').include? state
    'US'
  elsif Carmen.state_codes('CA').include? state
    'CA'
  else
    nil
  end
end

This is rocking my specs right now, so I think my issue is solved.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:15:53+00:00Added an answer on May 30, 2026 at 10:15 pm
    class Photo < ActiveRecord::Base
    
      attr_accessor :location
    
      def self.location_hash location
        city,state,country = location.split(",")
        country = "US" if country.blank?
        {:city => city, :state => state,  :country => :country}
      end
    
    end
    

    Now you can ‘find_or_create_by_*’

    Photo.find_or_create_by_name(
      Photo.location_hash(location).merge(:name => "foor bar")
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a kind of tricky layout in my app, so the main problem
I have kind of a tricky Oracle problem. I am trying to select one
Got kind of a tricky problem. I'm working on a project where we need
I have a kind of basic (but still unresolved) problem. I'm building a program
I have some kind of problem with jQuery selectors. Let's say i want to
I have a little DB challenge for you. Hopefully you can help. The Problem:
I'm currently working on my Referral System, but I have a problem with protecting
I have kind of reports about students accounts is it possible to print it
I have kind of library which uses a bunch of its own perf counters.
I have a kind of complicated string, the form is xxxp+NUMyyy where xxx, NUM,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.