On Monday, March 5, 2018 at 9:00:38 AM UTC-8, [email protected] wrote:
>
> Hi everybody.
>
> Look at this code:
>
> class Foo < Sequel::Model
>   one_to_many :bars
> end
>
> class Bar < Sequel::Model
>   many_to_one :foo
> end
>
> f = Foo.new
> b = Bar.new
> b.foo = f
> f.bars                 # => []
>
> f2 = Foo.new
> b2 = Bar.new
> f.bars                 # Magic line!
> b2.foo = f2
> f2.bars                # => [b2]
>
>
> I think first case should return same that second case.
>
> We continue with the example:
>
> f2.save                # Foo object with id 1 and a bar child
>
> b3 = Bar.new(foo_id: 1)
> f3 = b3.foo
> f3.bars                # => [f2]
>
>
> In this case, I think it should return [f2, f3]
>
> Do you see these requirements logical? If we have reciprocal and object
> assignation, I think these are two annoying Sequel effects that go against
> "least surprise" principle.
>

No, I don't think this makes sense.  First, your example has errors:

b2.foo = f2
# raises Sequel::Error: associated object #<Foo @values={}> does not have a 
primary key

After fixing those issues, let's consider f3.bars.  f3 is basically just 
Foo[1], since b3.foo_id is 1.  Foo[1].bars doesn't return [f2], it returns 
[b2]. It should never return f2 or f3 because it returns instances of Bar, 
not of Foo.  If you are asking if f3.bars should return [b2, b3], no, 
because b3 hasn't been saved to the database yet.

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.

Reply via email to