Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-967

Feature request: default value on the :foreign_key attribute

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 12_01_17
    • Component/s: None

      Description

      Hello, currently there's no way to set :default value for the :foreign_key attribute, and sometimes it may be useful.

      1. Usage
        class Post
        belongs_to :user, default: lambda {User.current.id}

        end

      1. Implementation
        Mongoid::Relations::Metadata.class_eval do
        def foreign_key_default_with_options
        self[:default] || foreign_key_default_without_options
        end
        alias_method_chain :foreign_key_default, :options
        end

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: