#zipcode = ZipCodes.identify(params[:zip_code])
I am new to rails. Can anyone tell me what does the keyword 'identify' do. Is this related to active record query?
Thank you in advance.
The gem zip-codes provides the ZipCodes class with a identify class method that simply reads a zip code and returns the state, city and timezone.
ZipCodes.identify('30301')
# => {:state_code=>"GA", :state_name=>"Georgia", :city=>"Atlanta", :time_zone=>"America/New_York"}
How do I/you know this? I guess you are new to the project where you saw this line:
First look inside your project? Do you have a class called ZipCodes? Yes, look in there.
No, look into your Gemfile, do you have anything mentioning ZipCodes or similar? Yes, search for the gem documentation.
No, Google a code snippet ZipCodes.identify and search among the results.
Here is the documentation for zip-codes: https://github.com/monterail/zip-codes
Related
In my rails app I want to use country-code, currency-code, ISO locale code to fetch some data from API. How can I get this information dynamically when user visit my site from anywhere?
I have used geocoder gem so by request.location I will get location's information and using this gem I can get country-code. Now I am not getting how can I get remaining information such as currency-code & ISO locale code?? Can anyone please help me or guide me??
I have seen this money gem but not sure it will provide me all these information.
Thanks in advance :)
I have tried #Prakash Murthy's answer. But there are many issue in this http://www.currency-iso.org/dam/downloads/table_a1.xml I found there is not proper name of all countries and some country has multiple currency_code which made me confused. But finally I found the solution by this single countries gem without creating any database.
Here is how I achieved the solution:
country_name = request.location.data['country_name'] # got country name
c = Country.find_country_by_name(country_name) # got currency details
currency_code = c.currency['code'] # got currency code
Sorry to answer my own question but I have posted here so in future if anyone stuck like me for the same issue then his/her time not wasted.
I found a way that makes it really easy:
Add currency gem to Gemfile, then bundle install
def currency_for_country(currency_iso_code)
ISO3166::Country.new(currency_iso_code).currency_code
end
Then:
currency_for_country('US')
=> "USD"
currency_for_country('AU')
=> "AUD"
This info is based off the countries gem readme
currency-code & ISO locale code are static data which change very rarely - if at all, and are best handled as static information within the system by storing them within the database tables. Might even be a good idea to provide a CRUD interface for managing these data.
One possible source for Currency code : http://www.currency-iso.org/en/home/tables/table-a1.html
List of All Locales and Their Short Codes? has details about getting the list of all locale codes.
In many Rails examples I see
ModelName.find(:id) being used, supposed every model has a unique string attribute named :symbol, how can I set up the model so that find(:symbol) would work? Do I have to implement the searching algo myself?
You could use
ModelName.find_by_symbol("uniquestring")
More info on Rails Dynamic Finders
You might want to try "find_by_yoursymbol"
ModelName.find_by_yoursymbol("symbol value")
I just used this to check if a record exists and to create it if it isn't there.
Spree::MailMethod.create(:environment => "test") unless Spree::MailMethod.find_by_environment("test").present?
I am trying to search my database before I enter the record, by doing this:
Product.update_or_create_by_name_and_date_and_applicationURL_and_server_and_addi_servers(app_name, app_date,url_app,server_name,addi_servers)
the problem is that I get an undefined method exception!
Is there another way to search for the same record before entering one?
You should use two steps:
#Suggestion 1
obj = Product.find_or_create_by_...
#Suggestion 2
obj = Product.find_or_initialize_by_...
obj.update_attributes hash_here
Rereading, your question, I can't really understand what do you want to update if you try to find an object with known attributes. Anyway, you would just have to adapt my answer a little if some fields are for identifying and some for update.
I would define a function in your model: something like
Product.find_by_everything
where you write out all the parameters of the search, instead of using the the long naming method.
Then, if that returns nil, create the product. This doesn't seem to be a good use case of using the built in activerecord naming methods.
I'm working on implementing a search form in a ruby on rails application. The general idea is to use form_tag to submit the search fields (via params) to a search function in the model of the class I'm trying to search. The search function will then iterate through each of the params and execute a scoping function if the name of the function appears in params.
The issue is that when I call the search on a collection like so:
#calendar.reservations.search({:search_email => "test"})
I don't know how to refer to the collection of #calendar.reservations from within the search function.
Additionally I'm confused as to why #calendar.reservations.search(...) works, but Reservations.all.search gives me an error saying you can't call an instance method on an array.
I've got the details of the search method over here: https://gist.github.com/783964
Any help would be greatly appreciated!
I don't know how to refer to the
collection of #calendar.reservations
from within the search function.
If you use self (or Reservation, it's the same object) inside the classmethod, you will access the records with the current scope, so in your case you will see only the reservations of a particular calendar.
[edit] I looked at you search function, and I think what you want is:
def self.search(search_fields)
search_fields.inject(self) do |scope, (key, value)|
scope.send(key, value)
end
end
Additionally I'm confused as to why
#calendar.reservations.search(...)
works, but Reservations.all.search
gives me an error saying you can't
call an instance method on an array.
#calendar.reservations does not return a standard array but a (lazy) AssociationCollection, where you can still apply scopes (and classmethods as your filter). On the other hand Reservation.all returns a plain array, so you cannot execute search there (or any scope, for that matter).
You don't really need a search method at all, as far as I can tell.
Simply use where:
#calendar.reservations.where(:search_email => 'test')
I would strongly encourage you to look at the MetaSearch GEM by Ernie Miller. It handles the kind of thing you're working on very elegantly and is quite easy to implement. I suspect that your view code would almost accomplish what the GEM needs already, and this would take care of all your model searching needs very nicely.
Take a look and see if it will solve your problem. Good luck!
Reservation.all.search doesn't work because it returns all the results as an array, while Reservation.where(..) returns an ActiveRecord object (AREL). Reservation.all actually fetches the results instead of just building the query further, which methods like where, limit etc do.
I want to build a rails query like the following. And would like to learn how so I can do it again on my own.
Pseudo Code:
#usersprojects = ?? Do I get record or just objects here? not sure?
SELECT *
FROM audit_log
WHERE project_id IN (#usersprojects)
IN the where IN () do I some how tell Rails to use the record.id?
thank you so much for helping me learn this. I want to Rails!
#kchau's answer was close, but you need to map out the project_id's from those records, like so:
AuditLogs.find(#userprojects.map(&:project_id))
So, if #usersprojects contained the User's Projects that you're trying to find audit logs for, you would do a query like:
logs = AuditLogs.find(#userprojects)
See the Rails guide for reference.