Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/tapioca/dsl/compilers/active_record_relations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,7 @@ def create_common_methods
constant,
column_type_option: Tapioca::Dsl::Helpers::ActiveRecordColumnTypeHelper::ColumnTypeOption::Persisted,
)
primary_key = constant.primary_key
getter_type, _setter_type = column_type_helper.type_for(primary_key)
getter_type, _setter_type = column_type_helper.type_for("id")
type = getter_type
create_common_method("ids", return_type: "T::Array[#{type}]")
else
Expand Down
48 changes: 48 additions & 0 deletions spec/tapioca/dsl/compilers/active_record_relations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,54 @@ def not(opts, *rest); end
assert_equal(expected, rbi_for(:Post))
end

it "generates typed ids for a nonstandard scalar primary key" do
add_ruby_file("schema.rb", <<~RUBY)
ActiveRecord::Migration.suppress_messages do
ActiveRecord::Schema.define do
create_table :posts, id: :string, primary_key: :code
end
end
RUBY

add_ruby_file("post.rb", <<~RUBY)
class Post < ActiveRecord::Base
end
RUBY

expected = indented(<<~RBI, 4)
sig { returns(T::Array[::String]) }
def ids; end
RBI

assert_includes(rbi_for(:Post), expected)
end

it "generates typed ids for composite primary keys" do
add_ruby_file("schema.rb", <<~RUBY)
ActiveRecord::Migration.suppress_messages do
ActiveRecord::Schema.define do
create_table :posts, primary_key: [:listing_id, :checkin_on, :checkout_on] do |t|
t.string :listing_id, null: false
t.date :checkin_on, null: false
t.date :checkout_on, null: false
end
end
end
RUBY

add_ruby_file("post.rb", <<~RUBY)
class Post < ActiveRecord::Base
end
RUBY

expected = indented(<<~RBI, 4)
sig { returns(T::Array[[::String, ::Date, ::Date]]) }
def ids; end
RBI

assert_includes(rbi_for(:Post), expected)
end

it "generates handles composite primary keys" do
add_ruby_file("post.rb", <<~RUBY)
class Post < ActiveRecord::Base
Expand Down
Loading