On Oct 19, 4:02 pm, Shawn <[email protected]> wrote:
> It would also be really cool to be able to create alternate views of a
> model, including joins to other tables, and have them be accessible
> through any association into that model. For example:
>
> class Contact < Sequel::Model
> def with_phone_numbers
> self.join(:phone_numbers, :contact_id => :id)
> end
> end
>
> class Supplier < Sequel::Model
> one_to_many :contacts
> one_to_many :contacts_with_phone_numbers, :association
> => :contacts, :via => :with_phone_numbers
> end
>
> class Customer < Sequel::Model
> one_to_many :contacts
> one_to_many :contacts_with_phone_numbers, :association
> => :contacts, :via => :with_phone_numbers
> end
>
> my_supplier.contacts_with_phone_numbers.each {...}
> my_customer.contacts_with_phone_numbers.each {...}
>
> That syntax might not be perfect, but it explains the requested
> feature, that the details of the view (and any complicated join logic)
> get stored once in the target model and not repeated in the
> definitions for the multiple associations into that model.
Can't you already do this if you define dataset methods in the model
class?:
class Contact < Sequel::Model
def_dataset_method(:with_phone_numbers) do
join(:phone_numbers, :contact_id => :id)
end
end
class Supplier < Sequel::Model
one_to_many :contacts
one_to_many :contacts_with_phone_numbers, :class=>:Contact do |ds|
ds.with_phone_numbers
end
end
class Customer < Sequel::Model
one_to_many :contacts
one_to_many :contacts_with_phone_numbers, :class=>:Contact do |ds|
ds.with_phone_numbers
end
one_to_many :contacts_named_jeremy_with_phone_numbers, :class=>:Contact
do |ds|
ds.filter(:name=>'Jeremy').with_phone_numbers
end
end
Jeremy
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---