I'm interested in pushing inserts and updates of many_to_many
associations into models. I have the following code at the moment. Is
there a better approach to this? I am using a multiple select in the
HTML form.

Regards,

Jamie Hodge

--

module App
  module Model
    class Collection < Sequel::Model

      plugin :validation_helpers
      plugin :timestamps, update_on_create: true
      plugin :association_dependencies

      one_to_many :documents
      many_to_many :keywords
      many_to_many :authors

      add_association_dependencies \
        documents: :destroy, keywords: :nullify, authors: :nullify

      attr_reader :author_ids, :keyword_ids

      def validate
        super
        validates_presence [:title, :description, :copyright]
        validates_length_range 1..255, [:title, :description]

        validates_integer :copyright
        validates_exact_length 4, :copyright
      end

      def author_ids=(value)
        @author_ids = value
        modified!
      end

      def keyword_ids=(value)
        @keyword_ids = value
        modified!
      end

      def after_save
        super

        remove_all_authors
        Array(author_ids).each {|id|
add_author(App::Model::Author[id]) }

        remove_all_keywords
        Array(keyword_ids).each {|id|
add_keyword(App::Model::Keyword[id]) }
      end

    end
  end
end

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