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
etc...
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[<%= group.id
%>]" >
# a checkbox for each group, to be chosen by the
searcher-guy
<% 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 << group.id if params["group_finder"]
!= nil &&
params["group_finder"]["#{group.id}"] == "on"}
groups = a.join(",")
if groups == '''' # if no checkbox was selected
@users = User.find(:all, :conditions => "group_id is null",
:include =>
:groups)
else #
@users = User.find(:all, :conditions => "group_id IN (#{groups})",
:include => :groups)
end
# performs the search on the given results, needs to be modified asap..i
know
@users = @users.find_all { |user| user.to_a.join.include? ''#the
query#''
}
render :action => ''index''
end
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...
--
Posted via http://www.ruby-forum.com/.