Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relation#last removes combines #431

Open
cllns opened this issue Jul 24, 2024 · 1 comment
Open

Relation#last removes combines #431

cllns opened this issue Jul 24, 2024 · 1 comment

Comments

@cllns
Copy link

cllns commented Jul 24, 2024

Describe the bug

Using #last on a Relation, after a using #combine, ignores the combine and just returns the regular tuple.

To Reproduce

require "rom"

Types = Dry.Types()
module Structs
  class Article < ROM::Struct
    # Including `title` and `body` is optional, we are able to declare struct-only attributes
    # If you comment these two lines out, we get the same result and same error
    attribute :title, Types::String
    attribute :body, Types::String
    attribute :favorites_count, Types::Integer
  end
end

rom = ROM.container(:sql, "sqlite::memory") do |conf|
  conf.default.create_table(:articles) do
    primary_key :id
    column :title, String, null: false
  end

  conf.default.create_table(:tags) do
    primary_key :id
    foreign_key :article_id
    column :value, String, null: false
  end

  conf.relation(:articles) do
    schema(infer: true) do
      associations do
        has_many :tags
      end
    end
  end

  conf.relation(:tags) do
    schema(infer: true) do
      associations do
        belongs_to :article
      end
    end
  end
end

articles = rom.relations[:articles]
tags = rom.relations[:tags]

articles.combine(:tags).command(:create).call(title: "First", tags: [{value: "worst"}])
articles.combine(:tags).command(:create).call(title: "Second", tags: [{value: "best"}])

puts "✅ articles.combine(:tags).first:"
puts "  " + articles.combine(:tags).first.inspect

puts

puts "❌ articles.combine(:tags).last: (MISSING TAGS!)"
puts "  " + articles.combine(:tags).last.inspect

puts

puts "✅ articles.combine(:tags).reverse.first:"
puts "  " + articles.combine(:tags).reverse.first.inspect

Outputs:

 articles.combine(:tags).first:
  {:id=>1, :title=>"First", :tags=>[{:id=>1, :article_id=>1, :value=>"worst"}]}

 articles.combine(:tags).last: (MISSING TAGS!)
  {:id=>2, :title=>"Second"}

 articles.combine(:tags).reverse.first:
  {:id=>2, :title=>"Second", :tags=>[{:id=>2, :article_id=>2, :value=>"best"}]}

Expected behavior

I would expect it would work just like articles.combine(:tags).reverse.first.

My environment

  • Rom-sql 3.6
  • Affects my production application: No
  • Ruby version: 3.3
  • OS: macOS 14.4.1

Related issues

#64

@flash-gordon
Copy link
Member

I think it'd be better to deprecate first/last on SQL relations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants