RailsCasts Pro episodes are now free!

Learn more or hide this

3dfxit's Profile

GitHub User: 3dfxit

Comments by

Avatar

Hi,

in my project I use meta_search to implement serching in my model

ruby
class Picture < ActiveRecord::Base
  include Rails.application.routes.url_helpers
  mount_uploader :avatar, AvatarUploader

  attr_accessor :prefix, :tag_list
  
  acts_as_ordered_taggable_on :tags

...

end

My controller is:

ruby
class PicturesController < ApplicationController

  def index
    if !params[:search].nil?
      @avatarSelected     ||= params[:search][:avatar_like].to_s
      @teggedSelected     ||= params[:search][:tagged_with_like].to_s
    end
    
    @search = Picture.search(:avatar_or_tag_taggings_tag_name_contains => params[:search])
    
    @search.meta_sort ||= 'created_at.desc'
    
    @pictures = @search.paginate(:page => params[:page], :per_page => 20)
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @pictures }
    end
  end

My _search.html.erb is:

ruby
                <%= form_for @search, :url => pictures_path, :html => {:method => :get} do |f| %>
                <table border="0" width="98%" align="center">
                        <tr>
                                <td>
                                    <%= f.label :avatar_like, "Filename" %>
                                    <%= f.text_field :avatar_like, :value => @avatarSelected %>
                          </td>
                    <td>
                                    <%= f.label :tagged_with_like, "Tegged wiith" %>
                                    <%= f.text_field :tagged_with_like, :value => @taggedSelected %>
                          </td>
                  </tr>        
                  <tr>
                                <td rowspan="2" align="center">
                                    <%= f.submit "Find" %>
                                  </td>
                        </tr>
                </table>
                <% end %>

I try the avatar_or_tag_taggings_tag_name_contains to combine a search on picture model witdh taggings, but widthout success.
Can you help me to do this?

Tank in advance
Steve