Forum: Ruby on Rails conditions and joins and paginate, oh my!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8dad1ec4d769734583f45fbbee5cd009?d=identicon&s=25 Jeff Pritchard (jeffpritchard)
on 2008-12-19 04:33
I'm starting off with something that already works, and trying to add
another condition to it.  I just can't seem to find an example of the
proper syntax to make this happen.

In this case, I have users, and users have many contacts and contacts
have and belong to many groups. Up til now I've just been searching for
(and paginating) a set of contacts that belong to this user that are
"LIKE" my search term.  Now I want to also limit the selection by which
groups a contact belongs to.

So far I've been finding a set of contacts to show like this:

@contacts = @current_user.contacts.paginate(:all, :conditions => ["name
LIKE ?", search_term])

Now I want to add to that, a selection based on a group, so, in pseudo
syntax that might look something like this:

@contacts = @current_user.contacts.paginate(
                                         :all,
                                         :conditions => ["name LIKE ?",
search_term],
                                         :joins => :group,
                                         :more_conditions => {:groups =>
{:group.id => selected_group}})

How do I hook these two different types of conditions together?

Thanks!
5b6e58cabeba80bc6faf29b7f45aa622?d=identicon&s=25 shoni khan (Guest)
on 2008-12-19 08:23
(Received via mailing list)
Jeff,
          Visit this page, it will really help you in solving your
problem
http://my.opera.com/learnror/blog/pagination-with-...
In case of any queries you can reply on that blog or on this form any
time.

Thanks & Regards,
Shahroon Ali Khan
8dad1ec4d769734583f45fbbee5cd009?d=identicon&s=25 Jeff Pritchard (jeffpritchard)
on 2008-12-19 08:29
shoni khan wrote:
> Jeff,
>           Visit this page, it will really help you in solving your
> problem
> http://my.opera.com/learnror/blog/pagination-with-...
> In case of any queries you can reply on that blog or on this form any
> time.
>
> Thanks & Regards,
> Shahroon Ali Khan

thanks very much. I will check it out.

jp
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-19 11:35
(Received via mailing list)
On 19 Dec 2008, at 07:29, Jeff Pritchard wrote:

>> Shahroon Ali Khan
>
> thanks very much. I will check it out.

It is however terrible advice. Fetching all rows into memory and then
paginating that array could absolutely kill you.

You can merge conditions with the merge_conditions class method on
ActiveRecord::Base (which is private on 2.1, but not 2.2. You can
always call it from one of your model's class methods. Or you can just
build up array based conditions by hand.

Fred
8dad1ec4d769734583f45fbbee5cd009?d=identicon&s=25 Jeff Pritchard (jeffpritchard)
on 2008-12-19 17:02
Frederick Cheung wrote:
> On 19 Dec 2008, at 07:29, Jeff Pritchard wrote:
>
>>> Shahroon Ali Khan
>>
>> thanks very much. I will check it out.
>
> It is however terrible advice. Fetching all rows into memory and then
> paginating that array could absolutely kill you.
>
> You can merge conditions with the merge_conditions class method on
> ActiveRecord::Base (which is private on 2.1, but not 2.2. You can
> always call it from one of your model's class methods. Or you can just
> build up array based conditions by hand.
>
> Fred

Thanks Fred,
Will that merge_conditions method deal with different styles of
conditions?, like:
:conditions => ["name LIKE ?", search_term]
and
conditions => {:groups => {:group.id => selected_group}}

I don't know how to translate either one of these into the other form.

thanks,
jp
This topic is locked and can not be replied to.