On Saturday, March 14, 2015 at 3:40:18 AM UTC-7, Janko Marohnić wrote:
>
> I'm fairly new to Sequel, but I had a lot of experience with ActiveRecord.
>
> I have Quiz model, which has many Questions. I'm trying to do a search
> which returns quizzes, but which also searches associated questions (I
> know, the basics :P). Now, I'm really impressed with Sequel's joins,
> ActiveRecord really sucks when it comes to joins. So, I have something like
> this:
>
> Quiz.association_right_join(:questions).to_a
> # => [#<Quiz @values={:id=>73, :quiz_id=>152, ...>]
>
> What happens is that columns are kind of joined together, which I
> understand. But what I find strange is that questions take over, and
> quizzes retreat ("id" becomes "quiz_id"). I find this strange, because if
> I'm searching *quizzes*, shouldn't *questions* be the second-class
> citizens (that questions' "id" turn into "question_id")?
>
> Also, is it possible to disable joining values? Because I only want to do
> a query, I don't need questions' columns. Maybe a "where" subquery would be
> better for that, without joining? How would I write it?
>
The default selection in Sequel is *, so if you join tables, you get
columns from all tables, and if columns with the same name are in multiple
tables, columns in joined tables will overwrite columns in the main table.
Just like in SQL, if you are joining tables, you should set an explicit
selection:
Quiz.association_right_join(:questions).select_all(:quizzes).to_a
Alternatively, you could use the table_select or column_select plugins.
The subquery approach as you mention in a later email may be a better way
to handle things.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.