How to use find_by option in rails - ruby-on-rails-3

I am new to rails so please anybody explain me how to use find_by option in rails.
For instance the table name is City with the field of id,city_name and country_name.

You'd use this to find a record that matches just the city_name field:
City.find_by_city_name(city_name)
You'd use this to find a record that matches the city_name & country_name fields:
City.find_by_city_name_and_country_name(city_name, country_name)
You probably wouldn't search for a record that matches all fields, because if you have the ID, then you can just get it directly with that:
City.find(id)
Search this page for "find_by" for more details.

Related

Is there an easy method to tidy up a Database Column by separating the place and the post code?

I currently have this problem:
I own a database with different columns like name, adress, place and post code. Unfortunately I have some messy entries where the post code is empty but the place is built like this: PLACE POST CODE.
Is there a way to clean this mess up? Or do I have to do it manually?
The database is built on Informix.
Sample Data:
Customer Number: 12315 (Auto Incremental)
Name : Best Machines (Company Name)
Other Names : Germany
Street Adress : Best Road in Town No. 15
Post Code : 51691
Name of Place : Best City HERE IS THE PROBLEM: Because some are saved like Best City POSTCODE even though there are two separate Columns ( i.e. Best City 51691)
Country : Best Country
Thanks in advance,
Gusdl
You could query the rows that matches "*" + POST CODE and update the place with PLACE - POST CODE
This update makes this
update name_of_table set place = substr(place, 1, length(place)-length(post_code))
where place matches "*" || post_code
I tested in Informix 12.10 and makes what you want

Display Country with the most occurrences Rails 3

I have a country model and would like to display the country with the most occurrences, country names are held in the column 'mame', however the country db is pre populated and the relationship is a country
has_many recipes
and recipe
belongs_to country
So far I have
Country.group('name').order('count_name DESC').limit(1).count('name')
but this will not work will it as there are 1 of every country in the table? Do i need to do a count on the number of times the country_id is used? if so what would the syntax be for that? would it be
Recipe.group('country_id').order('count_country_id DESC').limit(1).count('country_id')
or using joins and select
Country.joins(:recipes).select('countries.*, count(country_id) as "country_count"').group(:country_id).order(' country_count desc')
Any pointers appreciated
You can do it by using queries. However, RoR has built in support to achieve the same. It is called Counter Cache.
I can explain here but I think it's better if you follow this screencast.
http://railscasts.com/episodes/23-counter-cache-column
This will give you very good idea how to use counter cache and get what you've tried to achieve.

iPhone Addressbook and contact id? does it ever change?

Each contact in the address book, has a unique Id,
1) will this Id ever change? if so when does it change? ie a user deletes a contact, will the other contact id change? how do we make sure of this? will not change now or in the future.
EDIT: would like to uniquely identify a contact, which id should I use as a reference?
The documentation says:
The recommended way to keep a
long-term reference to a particular
record is to store the first and last
name, or a hash of the first and last
name, in addition to the identifier.
When you look up a record by ID,
compare the record’s name to your
stored name. If they don’t match, use
the stored name to find the record,
and store the new ID for the record.
Have one unique field like contact or address and compare the record's name with that field as well as id.

On which field was the keyword found? SQL Searching

I'm trying to make a detailed search engine for my web site.
The keywords are being searched in multiple fields and tables. For example, using keywords:
uludag
university
These keywords are being searched in the education, address, contactname, and contactsurname fields in my Members table.
I have to do it so because there must be only one input field for user.
Everything is fine until here, what I want to do is to show the user that this keyword was found on which field? I want to show a field named "Hits".
I'm using SQL Server 2008 and Full-text searching.
You can see an example of what I want to do from xing advanced search section.
You'll know in which field it is found because you know in which field you searched. If you don't care about which field contains the value, then you use the multi-column syntax:
SELECT ...
FROM Members
WHERE CONTAINS((education, address, contactname, contactsurname), 'uludag');
But if you want to search in a specific field then you have to specify only the field you're interested:
SELECT...
FROM Members
WHERE CONTAINS(education, 'uludag');
You can combine multiple fields and preserve the field of origin by unioning multiple queries:
SELECT 'education' as [field origin],...
FROM Members
WHERE CONTAINS(education, 'uludag')
UNION ALL
SELECT 'address', ...
FROM Members
WHERE CONTAINS(address, 'uludag')
...
UNION ALL
SELECT 'contactsurname', ...
FROM Members
WHERE CONTAINS(contactsurname, 'uludag');
And finally you can use the first form (search in all fields at once) and then check in the client which field contains the search term(s).

How to design a database table structure for storing and retrieving search statistics?

I'm developing a website with a custom search function and I want to collect statistics on what the users search for.
It is not a full text search of the website content, but rather a search for companies with search modes like:
by company name
by area code
by provided services
...
How to design the database for storing statistics about the searches?
What information is most relevant and how should I query for them?
Well, it's dependent on how the different search modes work, but generally I would say that a table with 3 columns would work:
SearchType SearchValue Count
Whenever someone does a search, say they search for "Company Name: Initech", first query to see if there are any rows in the table with SearchType = "Company Name" (or whatever enum/id value you've given this search type) and SearchValue = "Initech". If there is already a row for this, UPDATE the row by incrementing the Count column. If there is not already a row for this search, insert a new one with a Count of 1.
By doing this, you'll have a fair amount of flexibility for querying it later. You can figure out what the most popular searches for each type are:
... ORDER BY Count DESC WHERE SearchType = 'Some Search Type'
You can figure out the most popular search types:
... GROUP BY SearchType ORDER BY SUM(Count) DESC
Etc.
This is a pretty general question but here's what I would do:
Option 1
If you want to strictly separate all three search types, then create a table for each. For company name, you could simply store the CompanyID (assuming your website is maintaining a list of companies) and a search count. For area code, store the area code and a search count. If the area code doesn't exist, insert it. Provided services is most dependent on your setup. The most general way would be to store key words and a search count, again inserting if not already there.
Optionally, you could store search date information as well. As an example, you'd have a table with Provided Services Keyword and a unique ID. You'd have another table with an FK to that ID and a SearchDate. That way you could make sense of the data over time while minimizing storage.
Option 2
Treat all searches the same. One table with a Keyword column and a count column, incorporating SearchDate if needed.
You may want to check this:
http://www.microsoft.com/sqlserver/2005/en/us/express-starter-schemas.aspx