On Wed, Jun 16, 2021 at 6:22 PM Stuart Begg <[email protected]> wrote:
> I've been reading the association documentation for Sequel for some time > now. I'm trying to work out how to access an immutable data structure I > have for a process in our application. This is the scenario. > > To maintain an immutable history of the lifecycle of a user, I have two > main tables which are joined by a foreign key. Overtime, rows are added to > the user_stages table as the user progresses from states like 'pending' > to 'accepted', and on to (say) 'verified', or 'rejected', etc. The > relationship is as follows: > > # Database schema: > # users user_stages > # :id <----\ :id > # :auth_code \----- :user_id > # :status > > The class structure is as follows: > > class User > one_to_many :user_stage > This should probably be `one_to_many :user_stages`, as associations that return arrays should use the plural form. > one_to_one :user_stage > Assuming user_stages.id is an autoincrementing primary key, you probably want: one_to_one :user_stage, :order=>Sequel.desc(:id) That way it picks the most recent one. No need for a subquery, since ORDER BY id DESC LIMIT 1 will get the desired row. Since this is an association, usage would be like: user = User.first user.user_stage 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 view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/CADGZSSf_DUURmD5p7OhCf3DUOYGJc%2BpF8yhUuyXPmRhm4LE6TA%40mail.gmail.com.
