I did something like this by simply skipping over fields that ended in "_id" when auto-building forms. Dirt simple, but not especially recommended as "proper programming" but it got the (unpleasant/unwanted) job done and I went on to build better things more properly. :-)
Michael On Wed, Sep 1, 2010 at 5:35 PM, Dave Howell <[email protected]> wrote: > I needed to be able to figure out if a field of a model was a foreign key or > not, and couldn't figure out a way to do that with the existing Sequel > methods. I ended up resorting to this, which feels pretty bodgy: > > module Sequel > class Model > def self.foreign_keys > Hash[*DB["SELECT tc.constraint_name, kcu.column_name > FROM information_schema.table_constraints AS tc JOIN > information_schema.key_column_usage AS kcu using (constraint_name) > WHERE constraint_type = 'FOREIGN KEY' AND > tc.table_name='#{self.table_name}'"].collect{|row| > [row[:column_name].to_sym, > row[:constraint_name]] > }.flatten] > end > end > end > > > Why do I want to know? Because I'm building HTML forms for editing models, > and fields which are foreign keys should be ignored, whereas fields which are > names or dates or whatnot should get input boxes. > > I *think* there is an association for every foreign key on all my models, but > I'm not sure, because my associations have become rather complex; many of my > many-to-many linking tables contain critical characteristics for the > relationship, and Sequel's native behavior is to hide the entire linking > table, so I've had to create a number of intermediate views. > > -- > 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. > > -- http://codeconnoisseur.org -- 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.
