On Apr 13, 12:00 pm, Phil Crosby <[email protected]> wrote:
> Howdy,
>
> I'm joining two tables together which share two columns (created_at,
> updated_at). I'm joining them like this:
>
> movies = Movie.join(:movies_labels, :movie_id => :id)
>
> If I take the count of that dataset, "select *" is used, and since
> created_at is unqualified and ambiguous, it creates an error. I can
> work around this by qualifying the fields in the movies table
> (Movie.qualify.join(...)) but I don't want to do this because a lot of
> generic code (like pagination) depends on those movie fields being
> unqualified.
>
> Is there a way to qualify only the fields of the table being joined
> on, or is there another approach I should try?

First, a count should not use select *, it should use select count(*).
Unless you reference the created_at column in some way, I'm not sure
why you are getting an error.  You didn't provide the code, backtrace,
or SQL log, and it's hard to diagnose without those things.

Anyway, something like the following may work:

movies = Movie.select(:movies.*).join(:movies_labels, :movie_id
=> :id).select_more(*{DB[:movies_labels].columns.map{|x|
x.qualify(:movies_labels).as(:"movies_labels_#{x}")}})

If you find yourself needing this more than once, it's not hard to
code a generic dataset method that does the join+select_more.

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.

Reply via email to