El lunes, 5 de marzo de 2018, 20:55:40 (UTC+1), Jeremy Evans escribió:
>
> On Monday, March 5, 2018 at 9:00:38 AM UTC-8, David 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:
>

Arggg!

I am so sorry for the errors. I refer to [b2, b3], as you point.

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.
>

 I think reciprocal is a good concept, but I don't understand why it works 
in some cases and not in others. Loos at the first example. A simple f.bars 
before b2.foo = f2 assignment resolves well the reciprocal. But if you do 
without first loading bars, reciprocal assignation is avoided. It doesn't 
depend of persisted objects.

Second case is, pehaps, subtle, but I think the object representation (with 
reciprocals) doesn't depend in Sequel of persisted state, but in this case 
it seems so.

Thank you very much, and sorry again for errors :(

-- 
David

-- 
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