Commit 8a65723
Fix namespaced models foreign key (#460)
First of all, thank you very much for all the effort you put into this
gem. It is amazing. 🚀
## What this does
Version 1.8.2 breaks namespaced models with custom foreign keys.
Consider the following example:
```rb
ActiveRecord::Migration.create_table :support_conversations, force: true do |t|
t.string :model_id
t.timestamps
end
ActiveRecord::Migration.create_table :support_messages, force: true do |t|
t.references :conversation, foreign_key: { to_table: :support_conversations }
t.string :role
t.text :content
t.string :model_id
t.integer :input_tokens
t.integer :output_tokens
t.references :tool_call, foreign_key: { to_table: :support_tool_calls }
t.timestamps
end
ActiveRecord::Migration.create_table :support_tool_calls, force: true do |t|
t.references :message, foreign_key: { to_table: :support_messages }
t.string :tool_call_id
t.string :name
t.json :arguments
t.timestamps
end
```
```rb
module Support
def self.table_name_prefix
'support_'
end
class Conversation < ActiveRecord::Base
acts_as_chat message_class: 'Support::Message'
end
class Message < ActiveRecord::Base
acts_as_message chat: :conversation, chat_class: 'Support::Conversation', tool_call_class: 'Support::ToolCall'
end
class ToolCall < ActiveRecord::Base
acts_as_tool_call message_class: 'Support::Message'
end
end
```
```rb
Support::Conversation.last.messages
=> #<ActiveRecord::StatementInvalid:"PG::UndefinedColumn: ERROR: column support_messages.support_conversation_id does not exist
```
It does not work, since the correct foreign key should have been
`support_messages.conversation_id`.
I have added a test showcasing this and implemented a fix.
Since we cannot infer foreign keys automatically when it doesn't match
the model-name, we expose options to pass in a foreign key, similarly to
[associations in
Rails](https://guides.rubyonrails.org/association_basics.html#foreign-key):
```rb
class Support::Message < ActiveRecord::Base
acts_as_message chat_class: 'Conversation',
chat_foreign_key: 'conversation_id'
end
```
Generators have also been updated to add foreign keys if needed.
## Type of change
- [X] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation
- [ ] Performance improvement
## Scope check
- [X] I read the [Contributing
Guide](https://github.com/crmne/ruby_llm/blob/main/CONTRIBUTING.md)
- [X] This aligns with RubyLLM's focus on **LLM communication**
- [X] This isn't application-specific logic that belongs in user code
- [X] This benefits most users, not just my specific use case
## Quality check
- [X] I ran `overcommit --install` and all hooks pass
- [X] I tested my changes thoroughly
- [ ] For provider changes: Re-recorded VCR cassettes with `bundle exec
rake vcr:record[provider_name]`
- [X] All tests pass: `bundle exec rspec`
- [ ] I updated documentation if needed
- [X] I didn't modify auto-generated files manually (`models.json`,
`aliases.json`)
## API changes
- [ ] Breaking change
- [ ] New public methods/classes
- [X] Changed method signatures
- [ ] No API changes
## Related issues
Fixes bug introduced in #425
---------
Co-authored-by: Carmine Paolino <[email protected]>1 parent 76021b4 commit 8a65723
File tree
3 files changed
+111
-28
lines changed- lib
- generators/ruby_llm
- ruby_llm/active_record
- spec/ruby_llm/active_record
3 files changed
+111
-28
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
56 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
57 | 59 | | |
58 | 60 | | |
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
62 | 64 | | |
63 | 65 | | |
64 | | - | |
65 | | - | |
66 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
67 | 72 | | |
68 | 73 | | |
69 | 74 | | |
70 | 75 | | |
71 | 76 | | |
72 | 77 | | |
73 | 78 | | |
74 | | - | |
| 79 | + | |
| 80 | + | |
75 | 81 | | |
76 | 82 | | |
77 | 83 | | |
78 | 84 | | |
79 | 85 | | |
80 | 86 | | |
81 | 87 | | |
82 | | - | |
| 88 | + | |
| 89 | + | |
83 | 90 | | |
84 | 91 | | |
85 | 92 | | |
| |||
134 | 141 | | |
135 | 142 | | |
136 | 143 | | |
137 | | - | |
| 144 | + | |
138 | 145 | | |
139 | 146 | | |
140 | | - | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
141 | 155 | | |
142 | | - | |
| 156 | + | |
143 | 157 | | |
| 158 | + | |
144 | 159 | | |
145 | 160 | | |
146 | 161 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
35 | | - | |
| 34 | + | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
| 48 | + | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
| 71 | + | |
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
84 | | - | |
85 | | - | |
| 83 | + | |
86 | 84 | | |
87 | 85 | | |
88 | 86 | | |
89 | 87 | | |
90 | 88 | | |
91 | 89 | | |
92 | | - | |
93 | | - | |
94 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
95 | 93 | | |
96 | 94 | | |
97 | 95 | | |
| |||
106 | 104 | | |
107 | 105 | | |
108 | 106 | | |
109 | | - | |
| 107 | + | |
110 | 108 | | |
111 | 109 | | |
112 | 110 | | |
113 | 111 | | |
114 | | - | |
| 112 | + | |
115 | 113 | | |
116 | 114 | | |
117 | 115 | | |
| |||
126 | 124 | | |
127 | 125 | | |
128 | 126 | | |
129 | | - | |
| 127 | + | |
130 | 128 | | |
131 | 129 | | |
132 | 130 | | |
| |||
144 | 142 | | |
145 | 143 | | |
146 | 144 | | |
147 | | - | |
148 | | - | |
| 145 | + | |
| 146 | + | |
149 | 147 | | |
150 | 148 | | |
151 | 149 | | |
| |||
155 | 153 | | |
156 | 154 | | |
157 | 155 | | |
158 | | - | |
| 156 | + | |
159 | 157 | | |
160 | 158 | | |
161 | 159 | | |
162 | | - | |
| 160 | + | |
163 | 161 | | |
164 | 162 | | |
165 | 163 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
393 | 393 | | |
394 | 394 | | |
395 | 395 | | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
396 | 466 | | |
397 | 467 | | |
398 | 468 | | |
| |||
0 commit comments