before_create not invoked when object created

77 views Asked by At

I have a Lease Model:

class Lease < Sequel::Model
    def before_create
        self.issued_date = DateTime.now
        self.return_date = DateTime.now + 2.weeks
        super
    end
end

User Model:

class User < Sequel::Model
    many_to_many :items, left_key: :user_id, right_key: :item_id,
                 join_table: :leases
end

and Item Model:

class Item < Sequel::Model
    many_to_many :users, left_key: :item_id, right_key: :user_id,
                 join_table: :leases
end

My lease model has user_id and item_id as foreign keys.

When I create a Lease object the issued_date and return_date are created and saved in the database, but, when I assign a user with an item, a lease object is created but before_create is not invoked resulting in issued_date and return_date set to nil.

1

There are 1 answers

0
acolchagoff On

you need to set your before_create method like this:

before_create :before_create_method

def before_create_method
  #method body
end