Iain, Just to be clear, I wasn't trying to say that instance.field works for all fields of a join. It seems just those which were in the original Model derived dataset which was joined to. I'm not sure under what circumstances you could get them all to work that way. My practice is to specify all tables from their Models, but all further joined table fields seem to lose their model-like behavior even if the dataset used was originally specified from a Model object.
In any case, when in doubt I use the hash method which always seems to work on all fields of a join. Mike ________________________________ From: Iain Barnett <[email protected]> To: [email protected] Sent: Mon, November 7, 2011 4:04:40 AM Subject: Re: Model join returns a model instance, would prefer a hash On 3 November 2011 16:42, mhayden <[email protected]> wrote: In my usage I've noticed that accessors (instance.field) work on >joins derived from model objects, but the hash access methods >(record[:field]) will also work for all fields of the result whether >from the model or the joined table. So I don't think any change would >be required to get what you are asking for. >I've been using the hash method for accessing fields where the lower >level implementation may change, e.g direct SQL is required. >Mike > Thanks Mike, I didn't realise you could use the hash style accessors, that works for me but the instance.field type doesn't. I'm running Sequel v3.25.0, so maybe that's something to do with it, or a setting... but thanks very much for the help. >On Nov 3, 8:18 am, Jeremy Evans <[email protected]> wrote: >> On Nov 2, 10:26 pm, Iain Barnett <[email protected]> wrote: >> >> >> >> >> >> >> >> >> >> > Hi, >> >> > While trying out some new associations with dataset procs, I noticed that >> > joins to a model would return a result as an instance of that model. For >> > example: >> >> > User.join( Role ) >> > => #<Sequel::SQLite::Dataset: "SELECT * FROM `users` INNER JOIN >> > `roles`"> >> > User.join( Role ).first.class >> > => User >> >> > I would expect a hash, as in my mind it's equivalent to >> >> > DB[:users].join( DB[:roles] ) >> > => #<Sequel::SQLite::Dataset: "SELECT * FROM `users` INNER JOIN (SELECT >> > * FROM `roles`) AS 't1'"> >> > DB[:users].join( DB[:roles] ).first.class >> > => Hash >> >> > Is there an option to make it return a hash? Otherwise, the extra fields >> > don't have accessors as they're not defined on the model. I could use >> > `User.join( Role ).to_hash.first` but that isn't quite the same thing, and >> > my first thought would be that it added a lot of overhead, especially to >> > large datasets (but then I was wrong about the hash too! :) >> >> Dataset#naked does what you want (removes the row_proc that turns >> hashes into model objects). I wouldn't expect Model.join to remove >> the row_proc, I doubt most other Sequel users would expect it, and it >> would break backwards compatibility. If you want joins to remove the >> row_proc by default, you can easily do it yourself: >> >> Model.def_dataset_method(:join_table){|*a, &block| super(*a, >> &block).naked} >> >> Jeremy > Thanks Jeremy. I'll give this a try as well and see how it works out. There's just so many good options with this library! I really appreciate you taking the time to answer. Regards, Iain -- 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. -- 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.
