On Tuesday, January 22, 2019 at 1:09:46 PM UTC-8, Michael Faughn wrote: > > TL;DR -- I would like to be able to create models that were associated > with views. Is there a way to do this? > > Let's say I have the following kinds of Sequel::Model (and attendant > tables) with the attributes and associations listed: > > - Person -- name:String, current_address:Address > - DriverLicense -- issue_date:Date, expiration_date:Date, > address:Address, person:Person, number:int > - Address -- number:String, street:String, city:City > - State -- name:String > - City -- name:String, state:State > > Let's then say that I want a view that shows me just those people that > live in a certain city who have a currently valid driver license where the > address listed on the license is different than their current address. The > view should provide the person's name, license number, the address listed > on their license, and their current address. I know how to create a view > but all I know how to get back from it is a hash. I want to be able to get > a pile of PersonWithIncorrectAddressOnValidDriverLicense objects (of course > I would have declared this class already). I would also like to be able to > change the name or any part of the current address of a > PersonWithIncorrectAddressOnValidDriverLicense and have it update the > database correctly. Is there any existing way to do any of this and, if > so, how? >
Sequel works with both tables or views as backing for models (actually, you can use pretty much any dataset). If your database supports updateable views, you can update model instances for view-backed models. The only thing you probably need to use is to call set_primary_key appropriately in view-backed models. Sequel does this for you for table-backed models, but views don't have primary keys, so you need to call it manually. In terms of how to do this, if your view is named person_with_incorrect_address_on_valid_driver_licenses: class PersonWithIncorrectAddressOnValidDriverLicense < Sequel::Model end If you have a different name for the view: class PersonWithIncorrectAddressOnValidDriverLicense < Sequel::Model(:view_name) end If your database does not support updateable views or the particular view you create is not updateable, you'll probably want to override #save or a related internal method for the model to do what you want. 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 https://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
