Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using Rails 4 and postgresql database and I have a question about entering in a CSV dataset into the database.

Date    Advertiser Name Impressions Clicks  CPM     CPA     CPC     CTR
10/21/13    Advertiser 1    77         0    4.05    0.00    0.00    0.00
10/21/13    Advertiser 2    10732      23   5.18    0.00    2.42    0.21
10/21/13    Advertiser 3    16941      14   4.64    11.23   5.62    0.08
10/22/13    Advertiser 1    59         0    3.67    0.00    0.00    0.00
10/22/13    Advertiser 2    10130      15   5.24    53.05   3.54    0.15
10/22/13    Advertiser 3    18400      22   4.59    10.55   3.84    0.12
10/23/13    Advertiser 1    77         0    4.06    0.00    0.00    0.00
10/23/13    Advertiser 2    9520      22    5.58    26.58   2.42    0.23

Using the data above I need to create a show page for each Advertiser.

Ultimately I need to have a list of Advertiser's that I can click on any one of them and go to their show page and display the informations relevant to each advertiser (impressions, clicks, cpm, etc)

Where I am confused is how to import the CSV data when there are rows with duplicate Advertiser's, but the other columns contain relevant and non duplicate information. How can I set up my database tables so that I will not have duplicate Advertiser's and still import and then display the correct information?

share|improve this question
 
if there is a duplicate advisor, which row do you want to show? The most recent? or? –  portforwardpodcast 23 hours ago
 
I should have mentioned that the data needs to be displayed as a graph. All of the data related to an advertisor needs to be displayed over time. –  dhan 23 hours ago
add comment

1 Answer

You will want to create two models: Advertiser and Site. (or maybe date).

Advertiser "has many" Sites, and Site "has one" advertiser. This association will allow you to import your data correctly.

See: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

share|improve this answer
 
So after I create these two models of Advertiser and Date, how would I import the data? would it all just be within one import rake task or would it have to be separate? Do I just do the import in one task shown here? –  dhan 22 hours ago
 
I would probably create an import rake. You will need to loop through each line. I usually like to make find_or_create(id) methods. So for you this method would return an existing Advertiser, or a new one with that id. Then insert a Site and set the advertiser. I couldn't find a better video, but here is many-to-many (which is not what you have) railscasts.com/episodes/47-two-many-to-many –  portforwardpodcast 22 hours ago
 
You can use the link you gave as a starting place –  portforwardpodcast 22 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.