Here's what I wrote up on this subject in case anyone else finds it useful
...

http://steamcode.blogspot.com/2010/09/sequel-models-onetoone-associations.html

Let me know if I should make any corrections or clarifications.

Scott

On Tue, Sep 7, 2010 at 9:54 AM, Scott LaBounty <[email protected]> wrote:

> OK, I think I have it now.
>
> <<
> require 'rubygems'
> require 'sequel'
>
>
> DB = Sequel.sqlite
>
> DB.create_table :businesses do
>     primary_key :id
>     String :name, :text=>true, :unique=>true
>     foreign_key :address_id
>
> end
>
> DB.create_table :people do
>     primary_key :id
>     String :name, :text=>true, :unique=>true
>     foreign_key :address_id
>
> end
>
> DB.create_table :addresses do
>     primary_key :id
>     String :address
> end
>
> class Business < Sequel::Model
>     many_to_one :address
> end
>
> class Person < Sequel::Model
>     many_to_one :address
>
> end
>
> class Address < Sequel::Model
>     one_to_one :business
>     one_to_one :person
> end
>
>
> business = Business.create(:name => "The Business")
> person = Person.create(:name => "Bob")
>
> address_1 = Address.create(:address => "123 W. Lane, Anytown, CA 91110")
> address_2 = Address.create(:address => "124 W. Lane, Anytown, CA 91110")
>
> business.address = address_1
> person.address = address_2
>
> >>
>
> I actually had the foreign keys in the migration that I created this from
> (shouldn't do this sort of thing late at night). I looked at the one_to_many
> section of the documentation but didn't continue on to the "Differences
> Between" section which would have helped sort this out for me as would
> reading "Methods Added" closely.
>
> As always, thanks for the help.
>
> Scott
>
>
> On Tue, Sep 7, 2010 at 8:59 AM, Jeremy Evans <[email protected]>wrote:
>
>> On Sep 6, 7:52 pm, Scott LaBounty <[email protected]> wrote:
>> > I'm trying to use one to one associations where two tables can have one
>> to
>> > one associations with another table. Here's a simple example of what I'm
>> > trying to do ...
>> >
>> > <<
>> > require 'rubygems'
>> > require 'sequel'
>> >
>> > # Create an in-memory database
>> > DB = Sequel.sqlite
>> >
>> > DB.create_table :businesses do
>> >     primary_key :id
>> >     String :name, :text=>true, :unique=>true
>> > end
>> >
>> > DB.create_table :people do
>> >     primary_key :id
>> >     String :name, :text=>true, :unique=>true
>> > end
>> >
>> > DB.create_table :addresses do
>> >     primary_key :id
>> >     String :address
>> > end
>>
>> The first issue is that if you are using associations, you should have
>> foreign keys in your tables.
>>
>> > class Business < Sequel::Model
>> >     one_to_one :address
>> > end
>> >
>> > # Create the Employee model.
>> > class Person < Sequel::Model
>> >     one_to_one :address
>> > end
>> >
>> > class Address < Sequel::Model
>> >     one_to_one :business
>> >     one_to_one :person
>> > end
>>
>> The second problem is that a one to one relationship uses a
>> many_to_one association for the table that has the foreign key, and a
>> one_to_one association for the table the foreign key points to.
>>
>> > business = Business.create(:name => "The Business")
>> > person = Person.create(:name => "Bob")
>> >
>> > address_1 = Address.create(:address => "123 W. Lane, Anytown, CA 91110")
>> > address_2 = Address.create(:address => "124 W. Lane, Anytown, CA 91110")
>> >
>> > business.add_address(address_1)
>> > person.add_address(address_2)
>>
>> The third problem is that one_to_one associations act like many_to_one
>> associations, in that they use setter methods instead of add_/remove_
>> methods.
>>
>> > When I try to add the addresses I get ...
>> >
>> > <<
>> > onetotest.rb:43:in `<main>': undefined method `add_address' for
>> #<Business
>> > @values={:id=>1, :name=>"The Business"}> (NoMethodError)
>> >
>> >
>> >
>> > The documentation seems to imply that this should work.
>>
>> Please point out where so I can fix it.  The one-to-one relationship
>> is specifically discussed in
>> http://sequel.rubyforge.org/rdoc/files/doc/association_basics_rdoc.html,
>> where it says you should use many_to_one and one_to_one, and talks
>> about the foreign keys.
>>
>> 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]<sequel-talk%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/sequel-talk?hl=en.
>>
>>
>
>
> --
> Scott
> http://steamcode.blogspot.com/
>



-- 
Scott
http://steamcode.blogspot.com/

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

Reply via email to