Hi Jeremy, Thanks for your response! Yeah I figured it'd be the case, I've written a blog post <http://fredwu.me/post/58910814911/gotchas-in-the-ruby-sequel-gem>shortly after I posted my questions. If my assumption is correct - join and eager generate different SQL queries right?
On Thursday, 22 August 2013 01:35:50 UTC+10, Jeremy Evans wrote: > > On Wednesday, August 21, 2013 2:31:27 AM UTC-7, Fred Wu wrote: >> >> Hi everyone, >> >> I have a question regarding the behaviour of joined records - I have to >> confess that I don't use Sequel often, so my understanding of the Sequel >> API is rather limited and I apologise. The back story is that I'm >> implementing a Sequel data provider as I develop the gem called >> Datamappify <https://github.com/fredwu/datamappify>. >> >> Recently I've run into a weird issue where certain records are missing. >> After a few frustrating days and nights, I've finally found where the >> problem was - it was the different behaviour (compared to ActiveRecord) >> that got me. >> >> Long story short, in ActiveRecord, I could do: >> >> Post.joins(:author) >> >> Assuming a one-one relationship, that will get me all the posts with >> their corresponding authors. >> >> So naturally, after consulting the Sequel for ActiveRecord users >> guide<http://sequel.rubyforge.org/rdoc/files/doc/active_record_rdoc.html>, >> I did this in Sequel: >> >> Post.join(:authors, :id => :author_id) >> >> This is fine until I realised that the ID in Post records are being >> replaced by the ID from the corresponding Author records! As a result I had >> to do the following: >> >> Post.join(:authors, :id => :author_id).select(:posts__id, :posts__title, >> :posts__body) >> >> Is this the correct usage? I tried to look into the source code but >> didn't find a way to automatically add prefixes to the hash keys. >> > > The reason for this is that Dataset#join does not change the selected > columns (so they remain SELECT *). > > If you just care about elements in posts and not authors, you can do: > > Post.join(:authors, :id => :author_id).select_all(:posts) # SELECT > posts.* > > If you want to automatically set up unique aliases, there is Dataset#graph > (and the graph_each extension) or Dataset#eager_graph (though that does > eager loading). > > On a side note, I also found the eager loading API slightly more difficult >> to use than ActiveRecord - mainly due to the requirement of calling `all` >> on the end of the chain, otherwise the results would end up being hashes. :/ >> > > Well, there is the eager_each plugin if you don't want to have that > constraint, but then you end up with things like Dataset#first loading all > records just to return the first. > > Eager loading requires loading all records up front (Sequel defaults to > iteration), hence the default of requiring #all to be called. > > 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/groups/opt_out.
