我试图根据搜索表单中的参数搜索我的数据库。我在SQLlite中工作得非常好,但是当我迁移到postgres时,有几件事情破了。在app / views / index.html.erb中我定义了一个表单:
<tr>
<%= form_tag(guidelines_path, :method => "get", id: "search-form") do %>
<td><%= label_tag(:invprogsearch, "Search by Investor and Program")%></td>
<td><%= text_field_tag :investor, nil, placeholder: "Enter investor" %></td>
<td><%= text_field_tag :program_code, nil, placeholder: "Enter program" %></td>
<td><%= submit_tag "Search", :name => nil %></td>
<% end %>
</tr>
并在我的控制器(app / controllers / guidelines_controller.rb)中我将索引操作定义为
def index
if params[:investor].present? && params[:program_code].present?
@guidelines = Guideline.where(investor: params[:investor], program: params[:program_code])
else
@guidelines = Guideline.all
end
end
知道为什么这不起作用?我没有想法。谢谢!
应用程序/模型/ guideline.rb:
class Guideline < ActiveRecord::Base
validates :guide_desc, presence: true
belongs_to :parent, class_name: 'Guideline'
has_many :children, class_name: 'Guideline', foreign_key: 'parent_id'
scope :top_level, where(:parent_id => nil)
def descendants
self_and_descendants - [self]
end
def self_and_descendants
self.class.tree_for(self)
end
def self.tree_for(instance)
where("#{table_name}.id IN (#{tree_sql_for(instance)})").order("#{table_name}.id")
end
def self.tree_sql_for(instance)
tree_sql = <<-SQL
WITH RECURSIVE search_tree(id, path) AS (
SELECT id, ARRAY[id]
FROM #{table_name}
WHERE id = #{instance.id}
UNION ALL
SELECT #{table_name}.id, path || #{table_name}.id
FROM search_tree
JOIN #{table_name} ON #{table_name}.parent_id = search_tree.id
WHERE NOT #{table_name}.id = ANY(path)
)
SELECT id FROM search_tree ORDER BY path
SQL
end
end