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

Allow associations to be used inside defaults

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 8.0.1
    • Affects Version/s: None
    • Component/s: Associations
    • Labels:
      None
    • Minor Change

      Relations are set after defaults, so you can't use any relation method as default value.

      class Band
        include Mongoid::Document
        has_many :songs
        field :name
      end
       
      class Song
        include Mongoid::Document
        belongs_to :band
        field :band_name, default: -> { band.name }
        field :name
      end
       
      band = Band.create(name: "Molejo")
      song = band.songs.create(name: "Cilada")
      

      View as a gist

      It may be very useful, e.g.: denormalization.

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            dlibanori dlibanori
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: