0

3 番目の kms を介して関連付けられた、post と book の 2 つのモデルがあります。

関連付けは次のようになります。

class Post < ActiveRecord::Base
  attr_accessible :name, :year, :author, :book_ids
  has_many :kms
  has_many :books, through: :kms   
end

class Book < ActiveRecord::Base
  attr_accessible :name
  has_many :kms
  has_many :posts, through: :kms
end

class Km < ActiveRecord::Base
  attr_accessible :count, :book_id, :post_id
  belongs_to :book
  belongs_to :post

   def self.search(search)
      if search
        case type
           when "Name of Book"
                where('book.name ILIKE ?', "%#{search}%")
           when "Name OF Post"                                
                where('post.name LIKE ?', "%#{search}%")   
            end
      else
        scoped
      end            
  end
end

ここで_kms.html.erbは次のようになります。

<%= form_tag list_km_path, :method => 'get', :id => 'kms_search' do %>
    <p style="float:right;display:inline;">
        <%= hidden_field_tag :direction, params[:direction] %>
        <%= hidden_field_tag :sort, params[:sort] %>
        <%= select_tag "fieldtype", options_for_select([ "Name of Book", "Name of Post"], "Name of Book"),  :style => 'width:150px;' %>
        <%= text_field_tag :search, params[:search],  :style => 'width:200px;', :placeholder => "search..." %>
    </p>
<% end %>

<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered">
<thead>
    <tr>
       <th><%= sortable "book.name", "Name of Book" %></th>
       <th><%= sortable "post.name", "Name of Post" %></th>
    </tr>
  </thead >
  <tbody>
    <% for km in @kms %>
    <tr>
      <td><%= km.book.name %></td>
      <td><%= km.post.name %></td>

    </tr>
    <% end %>
  </tbody>
  </table>

ここでkms_controller.rbは次のようになります。

   def index
     @per_page = params[:per_page] || 10
     @kms  = Km.search(params[:search],params[:fieldtype]).order(sort_column + " " + sort_direction).paginate(:per_page => @per_page, :page => params[:page])
   end

  def sort_column
    Km.column_names.include?(params[:sort]) ? params[:sort] : "book.name"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"

問題 1 :

フォーム kms_search で書籍名と投稿名を検索したいのですが、fieldtype name of book を選択し、フィールドに書籍名と投稿名を入力しても機能せず、応答がありません。

問題 2 :

本の名前と投稿の名前を並べ替えたいのですが、エラーがあります:

PG::Error: ERROR:  missing FROM-clause entry for table "book"
LINE 1: ...kms" ORDER BY book...
                         ^
: SELECT  "kms".* FROM "kms" ORDER BY book.name asc LIMIT 10 OFFSET 0

どうすれば問題を解決できますか?

ありがとう。

4

1 に答える 1

1

SQL でクエリを実行するときは、すべてのテーブル名を複数形にします。

where('books.name LIKE ?', "%#{search}%")

確率2。

これでうまくいくかどうかはわかりませんが、次のようにしてみてください。

   <th><%= sortable "books.name", "Name of Book" %></th>
   <th><%= sortable "posts.name", "Name of Post" %></th>

def sort_column
    params[:sort].downcase == "posts.name" ? "posts.name" : "books.name"
end

そしてあなたのインデックスアクションで:

Km.search(params[:search],params[:fieldtype]).joins(:book).joins(:post).uniq.order(sort_column + " " + sort_direction)
于 2013-04-16T17:47:43.963 に答える