私はいくつかの異なるモデルでRoRデータベースを構築しました。各 "レコード"には多くのタグ付けがあり、 "cat_id"によってカテゴリに入れられます。私はすべてのモデルを作りましたが、私はフィルタリングの助けが必要です。今では、ノードを持つツリービューからカテゴリを選択する機能があり、そのカテゴリと子カテゴリのすべてのレコードが表示されます。私は検索機能(SQLのLIKE演算子)を使って簡単なテキスト検索を行うこともできます。カテゴリとタグを選択して結果を表示できるように、タグの追加フィルタを追加したいと思います。RoRタグなどの多対多関連に基づく検索
RECORDモデル
class AuctionRecord < ActiveRecord::Base
#comments
has_many :comments, :dependent => :destroy
#tags
has_many :auction_records_tags
has_many :tags, :through => :auction_records_tags
Tagモデル
class Tag < ActiveRecord::Base
attr_accessible :name
has_many :auction_records_tags
has_many :auction_records, :through => :auction_records_tags
end
AUCTIONS_RECORDS_TAGS MODEL
class AuctionRecordsTag < ActiveRecord::Base
attr_accessible :auction_record_id, :tag_id
belongs_to :tag
belongs_to :auction_record
end
レコードを見ることができるように:ここでは
は、私がこれまで持っているものですa ndタグには多対多の関係があるため、多くのタグを選択して多くのオークションレコードで返すことができます。
RECORD CONTROLLER
@auction_records = AuctionRecord.filter(session[:search], @cat_sql).order(sort_column + " " + sort_direction).paginate(:per_page => 20, :page => session[:page])
フィルタ機能
#this is the main search engine
def self.filter(search, cat_sql)
search_sql = ""
if search != ""
search_sql = "title LIKE '%#{search}%' OR itemnumber LIKE '%#{search}%' OR buyer LIKE '%#{search}%' OR seller LIKE '%#{search}%'"
end
if cat_sql != "" && search == ""
search_sql = cat_sql
end
if cat_sql != "" && search != ""
search_sql = search_sql + " AND " + cat_sql
end
if search_sql !=""
where(search_sql)
else
scoped
end
end
私は、すべてのノードと子供のノードをループのためにして手動でカテゴリのSQL文を生成します。あなたが見ることができるように、私はほとんどすべてを "手動で"(最良の方法ではありません)行います。特定のフィルタが定義されていないという事実は、厄介なものでした(例えばカテゴリ選択ですが、検索語はありません)。私は、3つのフィルタはすべて1行のコードで実現できると思っていますが、どうしたのかは分かりません。 tag.auction_recordsのようなものでしょうか?テーブルのスキーマを投稿する必要がある場合は、これもできますが、現在はすべてのカテゴリ化とタグ付けが整数/ ID関係で行われています。任意の助けを事前に
感謝/コメント
敬具
追加情報#1
ノードモデル
class Node < ActiveRecord::Base
has_many :nodes
belongs_to :node
end
SCHEMA
(分類/カテゴリを使用)create_table "auction_records", :force => true do |t|
t.string "title"
t.string "categorization"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "auction_records_tags", :force => true do |t|
t.integer "auction_record_id"
t.integer "tag_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "nodes", :force => true do |t|
t.integer "node_id"
t.string "text"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "tags", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
"auction_records"の場合、フィールド分類には、ツリービューに属するノードを指すnode_id整数が含まれます。単一のノード(カテゴリ)をクリックすると、多くのauction_recordsが表示されます。 auction_records_tagsは、auction_recordsとタグとの関連付けも作成します。私は1つのtag_idだけで動作するものを見つけましたが、複数のタグに対して何かをしなければなりません。
今、私はこのコードを持っており、それがエラーを返しているが、ノード「という名前の協会は 『
search_term = session[:search]
term = "%#{search_term}%"
@auction_records = AuctionRecord.scoped
@auction_records = @auction_records.where('title LIKE ?', term).where('description LIKE ?',term) if search_term
@auction_records = @auction_records.joins(:nodes).where('node.node_id IN ?', session[:cat_id]) if session[:cat_id]
@auction_records = @auction_records.joins(:tags).where('tags.id = ?', session[:tag_ids]) if session[:tag_ids]
@auction_records = @auction_records.order(sort_column + " " + sort_direction).paginate(:per_page => 20, :page => session[:page])
使用しているレールのバージョンはどれですか? –
Rails 3.0.6ありがとう! – user783437