2

i am using the carmen gem to store the country as their country code like for United states it is US . but when i retrieve the country in the view i retrieve it as their full name

i have a model user and account

class User < ActiveRecord::Base
      has_one :account
    end

 class Account < ActiveRecord::Base
      belongs_to :User
    end 

and

 @users = User.includes(:admin, :account).
       where.not(admin: { role: 'SUPER' }).where("names like ? OR accounts.country like ?" , "%#{ search }%", "%#{ search }%")

<% @users.each do |user| %>
  <%= user.names %>
  <%= Carmen::Country.coded(user.account.country).name %>
<% end %>

i have a user query which i have used in the view to find user names with their country but in the search like if i write US it gives me all the names of with country United states but i dont want to search the name with their country code , i want to write the whole country name

i know the issue is in the search query. the country code is stored in the database, so it is searching by the code , but i cant find out how to use carmen country code to their name conversion in the search query Please help , thankx in advance

user4965201
  • 973
  • 1
  • 11
  • 25
  • Not sure what your problem is here. Can you restate the question or provide an example of what is happening now and what you want to happen? – sshaw Aug 22 '15 at 02:40
  • @sshaw i am getting the Country short forms from accounts.country such as "US", "UK", "IN" and in search i want to write the full country name like "United States" so in where query i want to get full country name from the table – user4965201 Aug 24 '15 at 10:07
  • I still think I'm missing something. Why doesn't this (no error handling) work? `User.where('accounts.country like ?', "%#{Carmen::Country.coded(country).name}%")` – sshaw Aug 25 '15 at 19:59

1 Answers1

0

You can get Carmen Country object using fuzzy matching (actually use Regular Expression) by passing extra option fuzzy: true that is default false to Carmen::Country.named() method.

country = Carmen::Country.named('bang')

return nil but

  country = Carmen::Country.named('bang', fuzzy: true) 

return <#Carmen::Country name="Bangladesh">. Now you can get country 2 alpha code using country.alpha_2_code that return "BD" similarly alpha 3 code using country.alpha_3_code that you can use to you query.

N.B: Since here use fuzzy it will not give always right answer( I think most of the case :)) unless search key is close to actual word.

If I search Carmen::Country.named('ban', fuzzy: true) it return <#Carmen::Country name="Albania"> though I was expecting <#Carmen::Country name="Bangladesh">.

You can find about fuzzy in lib/carmen/querying.rb( my carmen version is 1.0.2). I think it may help someone.

Edited Though it's not best answer, you can save actual name to another column named like country_name which value will be set using after_save rails call back. Be careful about calling any active record update method with in callback that call callback method may cause infinite loop. You can use update_column that does not call callback.

Engr. Hasanuzzaman Sumon
  • 2,103
  • 3
  • 29
  • 38
  • doesnt work , i wanted to find US by giving united states as input but it gave UM in the case of United states UM is United states Minor Outlying Islands – user4965201 Oct 14 '15 at 10:09