My application has the concept of Users, Events, and Invitations. A user can be invited to many events, and an event can have many invited users; this relationship has a status attached to to it, which indicates whether the user has accepted the invitation or not.
I'm modeling the invitation by using an Invitation model for the join table between events and users, but I unable to actually update the join table through the Invitation model assigned to it:
class Event < Sequel::Model
many_to_many :users, :join_table => :invitations
one_to_many :invitations
end
class Invitation < Sequel::Model
many_to_one :user
many_to_one :event
end
class User < Sequel::Model
many_to_many :events, :join_table => :invitations
one_to_many :invitations
def invite (user, event)
user.add_invitation(event: event)
end
def decline(event)
i = Invitation.where(user: self, event: event).first
i.update(status: "declined")
end
end
user_one = User.create(name: "foo")
user_two = User.create(name: "bar")
event = Event.create(title: "fete")
user_one.add_event(event)
user_one.invite(user_two, event)
user_two.invitations
# [#<Invitation @values={:id=>nil, :status=>"pending", :event_id=>1, :user_id=>2}>]
# id is nil, which probably leads to the error below?
user_two.decline(event)
# `pk': No primary key is associated with this model (Sequel::Error)`
Aucun commentaire:
Enregistrer un commentaire