On Jun 8, 1:29 pm, Michael Lang <[email protected]> wrote:
> My natural inclination in a case like this would've been to do this:
>
> artists = []
> artists << Artist.load(:id=>1)
> artists << Artist.new
> artist_ids = artists.map{|a| a.id}.compact # [1]
> albums_for_artists = Album.filter(:artist_id=>artist_ids).all
>
> I say "natural inclination" because I actually do have code now where
> I compact the array to eliminate the nil values as I didn't want to
> risk looking for NULL's.
That way makes sense, but automatically using an OR for nil entries is
wrong.
> Today, I got frustrated with trying to add an "OR" clause to find
> specific values or IS NULL on a column within a rather complex
> existing query construct as I kept ending up with a full table list
> because the "OR" wasn't parenthesized quite right with the column I
> wanted to be either NULL or the specific values. I never did quite
> get it right and just ended up running the query twice and merging the
> results (due to my own short-comings in fully understanding how Sequel
> combines the ANDs and OR's) since I had to get the task done extremely
> quick for an upcoming meeting.
You should be able to do what you want using one of the following:
Album.filter({:artist_id=>artist_ids} | {:artist_id=>nil})
Album.filter([[:artist_id, artist_ids], [:artist_id, nil]].sql_or)
So your conditional code could look like:
cond = if artist_ids.all?
{:artist_id=>artist_ids}
else
{:artist_id=>artist_ids.compact} | {:artist_id=>nil}
end
Album.filter(cond)
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en.