GithubHelp home page GithubHelp logo

Comments (13)

solnic avatar solnic commented on August 20, 2024

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.

mrship avatar mrship commented on August 20, 2024

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.

solnic avatar solnic commented on August 20, 2024

@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.

solnic avatar solnic commented on August 20, 2024

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.

mrship avatar mrship commented on August 20, 2024

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.

solnic avatar solnic commented on August 20, 2024

@mrship what's your entity class?

from rom-sql.

mrship avatar mrship commented on August 20, 2024

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.

mrship avatar mrship commented on August 20, 2024

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.

solnic avatar solnic commented on August 20, 2024

from rom-sql.

mrship avatar mrship commented on August 20, 2024

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.

solnic avatar solnic commented on August 20, 2024

from rom-sql.

mrship avatar mrship commented on August 20, 2024

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.

solnic avatar solnic commented on August 20, 2024

@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)

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.