Comments (1)
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)
- "SQLite3::SQLException: row value misused" because of nested parenthesis HOT 1
- Possible race condition when inferring schema HOT 6
- Legacy schema
- Multi_insert returns more results than expected in some specific conditions HOT 5
- Attribute methods not available *within* a relation, but are *outside* of a relation HOT 4
- create returns nil for custom primary keys HOT 4
- OVER clause lost from row_number functions. HOT 9
- Postgres Upsert command does not support specifying index predicate for partial unique indices
- Translate symbols in a hash applied to `Relation#where` to attributes HOT 1
- "undefined method `stream'" when trying to use the `:pg_streaming` plugin HOT 2
- Multi-Schema database HOT 1
- Relation#multi_insert fails with blob data HOT 4
- Add a custom `Create` command for sqlite
- Use of projection dsl is not available in Relation#exclude
- Support for ANY in condition DSL HOT 5
- Error when attempting to join relations across different databases HOT 3
- [Migration] schema_migrations and schema_info tables error HOT 2
- Support virtual columns in `union` queries
- Problem with :create command and custom types HOT 2
- `by_pk` causes stack overflow when used with single primary key while table define composite primary key
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rom-sql.