Thank you Jeremy,
this should be even faster:

class Foo
def validate
super
errors.add(:bar_id, 'not matching bar') if bar_id && bar_dataset.empty?
end
end

Dne pátek 7. srpna 2020 18:03:03 UTC+2 Jeremy Evans napsal(a):
>
> On Friday, August 7, 2020 at 1:08:04 AM UTC-7, Petr Kaleta wrote:
>>
>> Hello everyone!
>>
>> I have project with two databases. Let's call them DB and SHARED_DB. For 
>> example I have two models:
>>
>> class Foo < Sequel::Model(DB[:foos])
>>   many_to_one :bar
>> end
>>
>>
>> class Bar < Sequel::Model(SHARED_DB[:bars])
>>   one_to_many :foos
>> end
>>
>>
>> I need to find some best way, how to validate during create/update that 
>> referenced bar_id really belongs to any bar... Traditional foreign_key in 
>> Postgresql is the best way but in my case we have two separate databases 
>> here. Have you ever solved the same problem?
>>  
>> Thank you
>>
>
> You can do something like:
>
> class Foo
>   def validate
>     super
>     errors.add(:bar_id, 'no matching bar') if bar_id && !bar
>   end
> end
>
> It's definitely not as good as a real foreign key, but I guess it is 
> better than nothing.
>
> 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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/d7b89763-8cd4-4ab7-9775-255b9a3ad37ao%40googlegroups.com.

Reply via email to