Git Product home page Git Product logo

Comments (1)

solnic avatar solnic commented on August 28, 2024

I thought it's a bug but it's actually not :) Here's the tweaked scripted that works:

require 'rom-sql'
require 'rom-repository'

module Relations
  class Users < ROM::Relation[:sql]
    schema(:users) do
      attribute :id, Types::Serial
      attribute :name, Types::String
      attribute :type, Types::String

      associations do
        has_many :journeys
      end
    end
  end

  class Drivers < ROM::Relation[:sql]
    register_as :drivers

    schema(:users) do
      attribute :id, Types::Serial
      attribute :name, Types::String
      attribute :type, Types::String

      associations do
        has_many :journeys
      end
    end
  end

  class Journeys < ROM::Relation[:sql]
    schema(:journeys) do
      attribute :id, Types::Serial
      attribute :name, Types::String
      attribute :user_id, Types::ForeignKey(:users)
      attribute :driver_id, Types::ForeignKey(:drivers)

      associations do
        belongs_to :user
        belongs_to :user, as: :driver, relation: :drivers
      end
    end
  end
end

module Repositories
  class Users < ROM::Repository[:users]
    commands :create
  end

  class Drivers < ROM::Repository[:drivers]
    commands :create

    def by_id(id)
      drivers.by_pk(id).one
    end
  end

  class Journeys < ROM::Repository[:journeys]
    commands :create

    relations :journeys, :users, :drivers

    def get_all
      journeys.to_a
    end

    def get_all_with_users
      aggregate(:user).to_a
    end

    def get_all_with_drivers
      aggregate(:driver).to_a
    end
  end
end

config = ROM::Configuration.new(:sql, 'sqlite::memory')
config.register_relation Relations::Users
config.register_relation Relations::Drivers
config.register_relation Relations::Journeys

container = ROM.container(config)

container.gateways[:default].tap do |gateway|
  migration = gateway.migration do
    change do
      create_table :users do
        primary_key :id
        string :name, null: false
        string :type, null: false
      end

      create_table :journeys do
        primary_key :id
        string :name, null: false
        integer :user_id
        integer :driver_id
      end
    end
  end
  migration.apply gateway.connection, :up
end

users_repo = Repositories::Users.new(container)
john = users_repo.create(name: 'John', type: 'user')

drivers_repo = Repositories::Drivers.new(container)
drivers_repo.create(name: 'Jerry', type: 'driver')
jerry = drivers_repo.by_id(2)
puts jerry.inspect

journeys_repo = Repositories::Journeys.new(container)
journeys_repo.create(name: 'Madrid-Barcelona', user_id: john.id, driver_id: jerry.id)

journeys = journeys_repo.get_all_with_users
puts journeys.inspect

journeys = journeys_repo.get_all_with_drivers
puts journeys.inspect

the output:

#<ROM::Struct[User] id=2 name="Jerry" type="driver">
[#<ROM::Struct[Journey] id=1 name="Madrid-Barcelona" user_id=1 driver_id=2 user=#<ROM::Struct[User] id=1 name="John" type="user" journey_id=1>>]
[#<ROM::Struct[Journey] id=1 name="Madrid-Barcelona" user_id=1 driver_id=2 driver=#<ROM::Struct[User] id=2 name="Jerry" type="driver" journey_id=1>>]

In general, the first argument in association macros must refer to the dataset name, in case of belongs_to (which just calls many_to_one under the hood) it is a singular version of the dataset that you want to "connect to", then it supports specifying which relation should be used via :relation option and you can alias it too via :as option, which will be used as the attribute name in loaded structs.

from rom-sql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.