Comments (13)
It doesn't work because inferred combine key is question_id
and it's missing in the resulting tuples. You can either specify the key manually using #combine
or you can still use combine_children
but you need to rename parent_id
to question_id
in for_questions
so something like that:
def for_questions(questions)
rename(parent_id: :question_id).where(parent_id: questions.pluck(:id), parent_type: "question")
end
Lemme know if that works. I should mention that in rom-repository 1.0.0 this will raise an error telling you that the combine key is missing and how you can make it work :)
from rom-sql.
That makes sense, and I had something similar at one stage where I did a select_append
question_id
but, with your changes, I then get:
ArgumentError: unknown keyword: question_id
from /Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/rom-repository-0.3.1/lib/rom/repository/struct_attributes.rb:39:in `initialize'
Implying that I can't create the ROM::Struct with the unknown key of question_id
.
I'll dig deeper.
from rom-sql.
@mrship ah right, this should be a view with a header:
view(:for_questions, %i[id question_id ... ]) do |questions|
rename(parent_id: :question_id).where(parent_id: questions.pluck(:id), parent_type: "question")
end
from rom-sql.
btw rom-sql deliberately doesn't support polymorphic associations because we do not want to promote features that throw data integrity out the window; however, since folks may have legacy databases, we can easily provide a plugin that you can enable that will setup associations for polymorphic relations by following how it's done in ActiveRecord.
from rom-sql.
OK, that helps in that I now do get a snapshot back (see below), however, I then try to map that result set as an entity, using .as(Question)
(where Question is an entity class) then it fails with:
#<TypeError: #<ROM::Struct[Snapshot] id=1 question_id=1 parent_type="question" figure_id=1 figure_type="charts" illustration="{}" created_at=2016-12-14 15:24:28 +0000 updated_at=2016-12-14 15:24:28 +0000> is not a symbol nor a string>
in rom/relation/loaded.rb
Any thoughts on why a mapping wouldn't work with that struct?
The successful bit :)
questions.combine_children(one: snapshots.for_question).to_a
=> [#<ROM::Struct[Question] id=1 topic_id=nil title="How are my employees performing?" short_title="Employee performance" clarification="Rejected opportunities are excluded from the achievement figures." methodology=nil sage_klass="Lighthouse::Sages::Questions::Simpleton" order=nil created_at=2016-12-14 12:11:15 +0000 updated_at=2016-12-14 12:11:15 +0000 snapshot=#<ROM::Struct[Snapshot] id=1 question_id=1 parent_type="question" figure_id=1 figure_type="charts" illustration="{}" created_at=2016-12-14 12:11:18 +0000 updated_at=2016-12-14 12:11:18 +0000>>]```
from rom-sql.
@mrship what's your entity class?
from rom-sql.
Sorry, I appreciate that it wasn't a particularly good error report re: the mapping; I'll try and isolate it further and provide some more detail.
from rom-sql.
My entity class is a Dry::Struct
, along the lines of:
class Question < Dry::Struct
constructor_type :schema
attribute :snapshot, Types::Snapshot.optional
attribute :charts, Types::Strict::Array.member(Chart).default { [] }
end
I have a custom type to create the correct snapshot object. I follow a similar pattern for charts (see above) that I pull back in my object graph with a combine_children/many, e.g.
questions
.combine_children(
many: {
charts: charts,
},
one: {
snapshot: snapshots.for_questions,
}
)
This works really well for the (many) charts, but not for one snapshot. Digging deeper it seems I'm being bitten by activesupport monkeypatching Object#try
(which is included via another gem I'm using). The (snipped) backtrace for the error shows that I end up in ActiveSupport Object#try when calling try
from dry-types. Interestingly I don't follow the same dry-struct code path for charts as it seems an Types::Strict::Array.member
doesn't run the try
method in the same way.
To be honest, I'm deep in the weeds here, so I'm not sure how to deal with this. I can make my snapshot an array (with one element) and it works fine (following the same pattern as for charts) but I'd prefer to keep it as a single relationship if possible.
Any thoughts? (Other than killing activesupport with extreme prejudice, which unfortunately isn't an option)
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/activesupport-4.2.7.1/lib/active_support/core_ext/object/try.rb:64:in `try'",
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/dry-types-0.9.3/lib/dry/types/sum.rb:76:in `block in try'",
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/dry-types-0.9.3/lib/dry/types/constrained.rb:44:in `try'",
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/dry-types-0.9.3/lib/dry/types/sum.rb:75:in `try'",
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/dry-types-0.9.3/lib/dry/types/sum.rb:70:in `call'",
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/dry-types-0.9.3/lib/dry/types/hash/schema.rb:75:in `block in coerce'",
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/dry-types-0.9.3/lib/dry/types/hash/schema.rb:88:in `block in resolve'",
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/dry-types-0.9.3/lib/dry/types/hash/schema.rb:86:in `each'",
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/dry-types-0.9.3/lib/dry/types/hash/schema.rb:86:in `resolve'",
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/dry-types-0.9.3/lib/dry/types/hash/schema.rb:73:in `coerce'",
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/dry-types-0.9.3/lib/dry/types/hash/schema.rb:27:in `call'",
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/dry-types-0.9.3/lib/dry/types/constructor.rb:39:in `call'",
"/Users/shipmana/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/dry-struct-0.1.1/lib/dry/struct/class_interface.rb:77:in `new'",
from rom-sql.
from rom-sql.
Aha! your comment about optional allowed me to progress. I can specify this as:
attribute :snapshot, Types::Snapshot.default { nil }
to get around the issue I have with ActiveSupport.
So, thanks for all the help. Let me also say that ROM is awesome. I'm enjoying the freedom to do DDD that it gives me.
It could definitely do with better documentation though :)
from rom-sql.
from rom-sql.
Sorry for the delay in replying, but yes that's exactly the issue I was having when declaring an attribute as .optional
I was ending up in ActiveSupport's try method and that was throwing an error about the ROM::Struct not being a symbol or a string.
from rom-sql.
@mrship OK I suspect it's a bug in structs + optional. I reported an issue in dry-struct dry-rb/dry-struct#26
from rom-sql.
Related Issues (20)
- 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
- SQLite doesn't cast 1/0 to true and false HOT 5
- Relation#last removes combines HOT 1
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.