Searchbar help!

Hi all,

i am after hours unfold of trying to implement a searchbar in a table
with a habtam relashinship, but alas ! with no success… i was hoping
some kind gentle mind can help me through these diffucult times; i have
now reached the point where out of sheer annoyance, i cannot see myself
getting anywhere nearer to the target, and was hopefully hoping one of
you out there could help me out…

so enough with the prologue; here is the story:

I have two tables joined with a has_and_belongs_to_many relashinship

    • users and groups ( and groups_users, accordingly).

in my view index.rhtml i have a list of users that each are associated
to a couple of groups (say:

user1: email1, username1, otherfields1, + g1 g2 g4
user2: email2, username2, otherfields2, + g1 g2 g4 g5
user3: email3, username3, otherfields3, + g1 g3 g4

my need is to implement a searchbar that will look through all the users
associated with the groups that were chosen, along with the query that
was inputted in the search field, and find the corresponding ones, and
display them.

the searchbar looks like this:

    <%= start_form_tag :action => 'search' %>
    <input type="text" name="search_bar" >
<% for group in @groups %>
   <input type="checkbox" name="group_finder[<%= %>]" >
        # a checkbox for each group, to be chosen by the 

<% end %>
<%= submit_tag ‘search’ %>
<%= end_form_tag %>

and then the controller action looks like this:

def search
a = []
@groups = Group.find(:all)

makes an array of ids of groups that were chosen on the searchbar

@groups.each { |group| a << if params[“group_finder”] != nil &&
params[“group_finder”]["#{}"] == “on”}
groups = a.join(",")
if groups == ‘’ # if no checkbox was selected
@users = User.find(:all, :conditions => “group_id is null”, :include =>
else #
@users = User.find(:all, :conditions => “group_id IN (#{groups})”,
:include => :groups)

performs the search on the given results, needs to be modified asap…i

@users = @users.find_all { |user| user.to_a.join.include? ‘#the query#’
render :action => ‘index’

i’ve reached the point after you’ve tried to do everything , and realize
you’ve done some stupid complex thing(s) where some one line code could
do the trick…but what line is it?? what am i missing? …the big
trouble i’ve had is trying to include a " title like ?,
#{params[:searchbar].gsub(’’, ‘%’)}etc along with a statement that will
take the habtam-connected groups…

if you think what i’ve done is way over complicated…

…you’re probably right.

so what do i do?

thank you so much for your help/sympathy…

I would say it would be better to pass the group ids in the form:

<% for group in @groups %> <%= %> <% end %>

Then you can just use that in the find:

groups = params[:groups]
@users = User.find(:all,
:conditions =>[‘group_id IN (?) AND title LIKE ?’,

This is un-tested code, but i hope it helps :0)


groups = params[:groups]
@users = User.find(:all,
:conditions =>[‘group_id IN (?) AND title LIKE ?’,

well, first of all, thanks for the reply…
my main problem is that in order for the :conditions clause to work, i
need to add the :include => ‘groups’ so that it changes the sql snippet
to a type “join”, and in that clause it doesn’t allow me to add a LIKE
%% statement…i may be missing a certain paranthesis or something, but
i haven’t been able to make it work…very aggrevating, because it seems
like it should work. i am relativley new at rubyonrails and so i’m
haven’t aquired enough confidence to know if my mistake is stupid error,
or if i’m shooting at the wrong target; however, i did come across a
vague document in api :base (rubydoc) that it is not possible to use the
LIKE command in a “JOIN” sql snippet (using :include) when in a habtam
but, as we all know that rails is an amazing framework, it is hard for
me to believe that there is no other solution…so, what should be my
next step? i’ve been pulling my hairs out for a day and a half now(among
other things)…

any help, references or whatever is greatly, greatly appreciated…


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs