On Jul 11, 1:40 am, Clive Crous <[email protected]> wrote:
> Hi,
>
> Imagine the scenarios where I have 3 tables => :foos, :bars, :foos_bars
> I have 2 (perhaps 3) models => Foo, Bar (, FooBar)
>
> and the Foo model looks like this =>
>
> class Foo < Sequel::Model
> many_to_many :bars, :join_table => :foos_bars, :class => 'Bar'
> end
>
> Within 'foos_bars' I have a column which describes the relationship
> between Foo and Bar so :foos_bars's schema is something like:
> {
> foreign_key :foo_id, :table => :foos, :null => false
> foreign_key :bar_id, :table => :bars, :null => false
> varchar relationship, :size => 32, :null => false
>
> }
>
> My problem is as follows, how can I use foo.add_bar when there's
> column data required in the joining table, for example:
> {
> foo = Foo.create( ... )
> bar = Bar.create( ...)
>
> # foo.add_bar( bar, :relationship => "baz" ) # Unfortunately this
> does not work
>
> foo.add_bar( bar ) # How do I do this? where do I describe relationship?
>
> }
There have been various hacks discussed, but the current best solution
is to use a real model for the join table:
class FooBar < Sequel::Model(:foos_bars)
many_to_one :foo
many_to_one :bar
end
class Foo
one_to_many :foo_bars
def add_bar(bar, values={})
foo.add_foo_bar(FooBar.new(values.merge(:bar=>bar)))
end
end
This isn't an optimal solution as callbacks, reciprocals, and caching
are probably not handled as expected.
One way to do what you want is to have the add_*/remove_*/remove_all_*
methods take additional arguments, and pass those arguments down to
_add_*/_remove_*/_remove_all_*. Then you could just do:
class Foo
def _add_bar(bar, values={})
DB[:foos_bars].insert(values.merge
(:foo_id=>id, :bar_id=>bar.id))
end
end
I'm not opposed to a patch that does that, assuming it came with
decent specs.
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
-~----------~----~----~----~------~----~------~--~---