0

I'm implementing a simple search form. What I wanna do is to search the course by title. My migration file for Course is lass CreateCourses < ActiveRecord::Migration

  def change
    create_table :courses do |t|
      t.integer :Course_num
      t.string :Title
      t.text :Description
      t.string :Instructor
      t.integer :Instructor_ID
      t.date :Start_date
      t.date :End_date
      t.boolean :Status

      t.timestamps null: false
    end
    add_index :courses, :Course_num, unique: true
  end
end

This is my course model:

class Course < ActiveRecord::Base
  def self.search(search)
    if search
      self.where("Title LIKE ?", "%#{search}%")
    else
      self.all
    end
  end
end

This is the index part of my controller:

def index
    @courses = Course.search(params[:search])
  end

And this is my course index view:

<div class="container">
  <div class="row col-md-4 col-md-offset-4" style="left: 0%; top: 70px;">
<p id="notice"><%= notice %></p>

<h1>Listing Courses</h1>
    <%= form_tag courses_path, :method => 'get' do %>
        <p>
        <%= text_field_tag :search, params[:search], placeholder: "Search Course" %>
        <%= submit_tag "Search", :name => nil %>
        </p>
    <% end %>

    <table>
  <thead>
    <tr>
      <th>Course num</th>
      <th>Title</th>
      <th>Description</th>
    </tr>
  </thead>

  <tbody>
    <% @courses.each do |course| %>
      <tr>
        <td><%= course.Course_num %></td>
        <td><%= course.Title %></td>
        <td><%= course.Description %></td>
        <td><%= link_to 'Show', course %></td>
        <td><%= link_to 'Edit', edit_course_path(course) %></td>
        <td><%= link_to 'Destroy', course, :method => :delete, :class=>:destroy, data: { confirm: 'Are you sure?' }%></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Course', new_course_path %>

    </div>
  </div>

And everytime I search, it always tells me that:

PG::UndefinedColumn: ERROR:  column "title" does not exist
LINE 1: SELECT "courses".* FROM "courses" WHERE (Title LIKE '%2%')
                                                 ^
HINT:  Perhaps you meant to reference the column "courses.Title".
: SELECT "courses".* FROM "courses" WHERE (Title LIKE '%2%')

I have no idea why, since I put "Title" instead of "title" in the model. What should I do? Thanks!

potashin
  • 44,205
  • 11
  • 83
  • 107
DevArenaCN
  • 131
  • 3
  • 17

1 Answers1

1

Try the following:

self.where('"courses.Title" LIKE ?', "%#{search}%")
potashin
  • 44,205
  • 11
  • 83
  • 107