# Multi select with conditions

**URL:** <https://www.ruby-forum.com/t/multi-select-with-conditions/135040>\
**Category:** Ferret\
**Created:** [April 17, 2008, 12:27pm UTC](https://www.ruby-forum.com/t/multi-select-with-conditions/135040 "2008-04-17T12:27:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [April 17, 2008, 12:27pm UTC](https://www.ruby-forum.com/t/multi-select-with-conditions/135040/1 "2008-04-17T12:27:18Z")

</div>

Hi

I’m doing a multi-model search like this, which works absolutely fine:

```
    @search_results = ActsAsFerret::find( "trumpet", [Resource, 

```

Lesson,  
Course],  
#(ferret) options  
{ :page =\> 1,  
:per\_page =\> 20,  
:sort =\>  
Ferret::Search::SortField.new(:name, :reverse =\> false)  
},  
#find options  
{}  
)

My new problem is that the results need to be restricted: basically any  
user  
has only limited access to certain lessons, resources and courses,  
depending  
on their shared priveleges. I have methods to produce arrays of ids of  
each  
that the user is allowed to see, eg

@user.allowed\_lessons  
=\> array of ids of all lessons the user has access to

Can anyone show me how to incorporate this into my search? I have the  
feeling that i can do something with the :conditions options, which can  
be  
passed through in the find options hash. But i can’t work it out: i  
tried  
this:

:conditions =\> [“lesson.id in (?) or course.id in (?) or resource.id in  
(?)”, @user.allowed\_lessons, @user.allowed\_courses,  
@user.allowed\_resources]

But it doesn’t work: it says ‘no such field as lesson.id in resources’.  
I  
feel like this should be possible - can anyone help?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [May 31, 2008, 6:28pm UTC](https://www.ruby-forum.com/t/multi-select-with-conditions/135040/2 "2008-05-31T18:28:45Z")

</div>

I have the same problem Max, did you solve it?

Regards!

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 12, 2008, 3:00am UTC](https://www.ruby-forum.com/t/multi-select-with-conditions/135040/3 "2008-06-12T03:00:08Z")

</div>

Hi Max,

Try this:

@search\_results = ActsAsFerret::find(“trumpet”, [Resource, Lesson,  
Course],  
#(ferret) options  
{ :page =\> 1,  
:per\_page =\> 20,  
:sort =\>  
Ferret::Search::SortField.new(:name, :reverse =\> false)  
},  
#find options - note this assumes  
it’s a flat array of allowed id numbers:  
{ :lesson =\> “lessons.id IN  
(#{@user.allowed\_lessons.join(’,’)})”,  
:course =\> “courses.id IN  
(#{@user.allowed\_courses.join(’,’)})”,  
:resource =\> “resources.id IN  
(#{@user.allowed\_resources.join(’,’)})”  
}  
)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 12, 2008, 5:31am UTC](https://www.ruby-forum.com/t/multi-select-with-conditions/135040/4 "2008-06-12T05:31:16Z")

</div>

Sorry, I missed the :conditions key in the last hash  
{ :conditions =\> {:lesson =\> “lessons.id IN  
(#{@user.allowed\_lessons.join(’,’)})”,  
:course =\> “courses.id IN  
(#{@user.allowed\_courses.join(’,’)})”,  
:resource =\> “resources.id IN  
(#{@user.allowed\_resources.join(’,’)})”  
}  
}

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [June 13, 2008, 11:31am UTC](https://www.ruby-forum.com/t/multi-select-with-conditions/135040/5 "2008-06-13T11:31:04Z")

</div>

Thanks all

On the way to solving this i found a bug in acts\_as\_ferret - it seems  
that if i try to sort, paginate and use conditions at the same time then  
the sorting breaks down: instead of being sorted and then paginated,  
the results are paginated (ordered simply by id) and then sorted within  
each page.

I let Jens K. (and the acts as ferret mailing list) know about it  
but as far as i know it’s not been fixed.

I ended up doing a ferret search to get the ids of the results (with  
unallowed records filtered out), and then doing an AR find to get those  
results, sort and paginate them. So, it’s a little inelegant but it  
works, at least.

thanks  
max
