Sha256: 0345933b340f4d9764d41281e011aeaf9b177fd7791c2dc1dbe0f22b7f60f5c2

Contents?: true

Size: 998 Bytes

Versions: 4

Compression:

Stored size: 998 Bytes

Contents

# Copyright 2023 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.

class Track < ActiveRecord::Base
  # `tracks` is defined as INTERLEAVE IN PARENT `albums`.
  # The primary key of `albums` is (`singerid`, `albumid`).
  # Rails 7.1 requires a composite primary key in a belongs_to relationship to be specified as query_constraints.
  belongs_to :album, query_constraints: [:singerid, :albumid]

  # `tracks` also has a `singerid` column that can be used to associate a Track with a Singer.
  belongs_to :singer, foreign_key: :singerid

  # Override the default initialize method to automatically set the singer attribute when an album is given.
  def initialize attributes = nil
    super
    self.singer ||= album&.singer
  end

  def album=value
    super
    # Ensure the singer of this track is equal to the singer of the album that is set.
    self.singer = value&.singer
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activerecord-spanner-adapter-1.6.3 examples/snippets/interleaved-tables/models/track.rb
activerecord-spanner-adapter-1.6.2 examples/snippets/interleaved-tables/models/track.rb
activerecord-spanner-adapter-1.6.1 examples/snippets/interleaved-tables/models/track.rb
activerecord-spanner-adapter-1.6.0 examples/snippets/interleaved-tables/models/track.rb