On Tue, Dec 7, 2010 at 8:40 AM, Jeremy Evans <[email protected]> wrote:

> On Dec 7, 6:14 am, Scott LaBounty <[email protected]> wrote:
> > All,
> >
> > I'm not sure how to do this in Sequel. I have tables/models for Users and
> > Requests so a User can create a request and there's a "one_to_many
> requests"
> > in the User model and a "many_to_one user" in the Request model. So far,
> so
> > good. I'd like to add the capability to allow users to "Like" (not what
> I'm
> > calling it, but it will at least be familiar from FaceBook). So there,
> we'd
> > have a many_to_many relationship between the requests and users (users
> could
> > "Like" many requests and requests could be "Like"d by many users). I'm
> not
> > sure how to do this since we already have a relationship between the two
> > models.
> >
> > Thoughts?
>
> It's possible to have multiple relationships between models, you just
> have to use different names:
>
>  class User
>    one_to_many :requests
>    many_to_many :liked_requests, :class=>Request
>  end
>
> 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.
>
>
OK, here's what I came up with ...

<<
require 'rubygems'
require 'sequel'

DB = Sequel.sqlite

DB.create_table :users do
    primary_key :id
    column :name, :text, :unique=>true
end

DB.create_table :requests do
    primary_key :id
    column :title, :text, :unique=>true
    foreign_key :user_id, :users
end

DB.create_table :liked_requests do
    primary_key :id
    foreign_key :request_id, :requests
    foreign_key :user_id, :users
end

class Request < Sequel::Model
end

class User < Sequel::Model
    one_to_many :requests
    many_to_many :liked_requests, :class => Request,
        :join_table => :liked_requests, :left_key => :user_id, :right_key =>
:request_id
end

class Request < Sequel::Model
    many_to_one :user
    many_to_many :liked_by_users, :class => User,
        :join_table => :liked_requests, :left_key => :request_id, :right_key
=> :user_id
end

# Create some users.
user_alice = User.create(:name => "alice")
user_bob = User.create(:name => "bob")
user_carol = User.create(:name => "carol")

# Create some requests.
request_love = Request.create(:title => "love")
request_money = Request.create(:title => "money")
request_respect = Request.create(:title => "respect")

# Add requests to the users.
user_alice.add_request(request_love)
user_bob.add_request(request_money)
user_carol.add_request(request_respect)


user_alice.add_liked_request(request_money)
user_alice.add_liked_request(request_love)
user_carol.add_liked_request(request_love)
user_bob.add_liked_request(request_money)

p user_alice.requests
p user_alice.liked_requests
p request_love.liked_by_users

>>

Now the only thing "weird" is needing to have the empty Request class before
doing the User class with the many_to_many. Not the worst thing in the
world, but it seems like there's probably a better way.

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