1

I have made significant progress in Rails since my last post. So here is what the goal is!

  1. Created "Course" objects from raked csv file.
  2. Access the attributes of those courses in order to print them onto a table.

The rake executes without any errors, so first QUESTION. How can I check to see if the objects were actually created?

Rake File:

require 'csv'
namespace :data do
desc "Imports data from course_listing.csv"
task :import_csv => :environment do 
CSV.foreach("db/course_listing.csv") do |row|
  Course.new(
    :ID => row[0],
    :type => row[1],
    :location => row[2],
        :meeting_days => row[3],
        :start_time => row[4],
        :stop_time => row[5],
        )
end
end
end

Next, here are my controller and model. All I need to do is print the attributes so the model should be barebones.

Controller:

class CoursesController < ApplicationController
def new
@courses = Course.new
end

def create
@course = Course.create(:ID, :type, :location, :meeting_days, :start_time, :stop_time)
end

def index
@courses = Course.all
end
end

Model:

class Course < ActiveRecord::Base
attr_accessor :ID, :type, :location, :meeting_days, :start_time, :stop_time
end

Lastly here is the view thats throws the exception "undefined method `each' for #Course id: nil, created_at: nil, updated_at: nil"

View:

<table border="1" style="width:50%">
<tr>
<th>Type</th>
<th>Location</th>
<th>Meeting Days</th>
<th>Start Time</th>
<th>Stop Time</th>
</tr>
<% @courses.each do |course| %>
<tr>
<td><%= course.type %></td>
<td><%= course.location %></td>
<td><%= course.meeting_days %></td>
<td><%= course.start_time %></td>
<td><%= course.stop_time %></td>
</tr>
<% end %>
</table>

So how can I fix this :) I am new to rails and am still getting used to it!

Thanks!

Kyle Brown
  • 303
  • 4
  • 14
  • I would split this up into multiple questions. StackOverflow is meant to build up an archive of questions others may also share. Bundling questions together not only makes them less likely to be answered, but also makes this question too specific to your project. – Chris Fritz Nov 13 '14 at 05:05

2 Answers2

0

I'm just going to answer your first question and you can edit to move the other questions into their own questions.

In general, if you want to check if objects are being created/updated in your DB, a quick way is to open up the rails console (by running rails console in the terminal) and doing a query for that data. See the Active Record Query Interface docs for details on queries you could run.

An even better way to test your rake tasks though would be with a testing library. Here's an intro to rspec and an example of specs for a rake task.

Finally, to save you a little time, that rake task will not actually save anything to the database. Check out this answer to another question to see why.

Community
  • 1
  • 1
Chris Fritz
  • 1,922
  • 1
  • 19
  • 23
0

First answer. You can create more strict and consistent model by setting up validations. During Rake task you can check process by using implicit save!, it will trow exception in any case if something goes wrong.

...
c = Course.new(...)
c.save!
...

Second question. Please find good explanation about nil/empty/false here. You can check field with trenary operator. Here is the example how to change you code:

<td><%= course.stop_time.nil? ? 'N/A' : course.stop_time %></td>

It will put 'N/A' in table cell if data missed.

Community
  • 1
  • 1
sashaegorov
  • 1,821
  • 20
  • 26