You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: add guide for using custom model names with acts_as helpers (#171)
While integrating RubyLLM with an existing Rails application that
already had a `Chat` model, I discovered that the framework supports
using custom model names with the `acts_as` helpers. This isn't
explicitly documented, but is a valuable feature for real-world Rails
applications where naming conflicts or existing conventions might exist.
Let me know if I'm missing something on this behaviour
---------
Co-authored-by: Carmine Paolino <carmine@paolino.me>
Copy file name to clipboardExpand all lines: docs/guides/rails.md
+42-3Lines changed: 42 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -163,7 +163,7 @@ Include the RubyLLM helpers in your ActiveRecord models:
163
163
classChat < ApplicationRecord
164
164
# Includes methods like ask, with_tool, with_instructions, etc.
165
165
# Automatically persists associated messages and tool calls.
166
-
acts_as_chat #Assumes Message and ToolCall model names
166
+
acts_as_chat #Defaults to Message and ToolCall model names
167
167
168
168
# --- Add your standard Rails model logic below ---
169
169
belongs_to :user, optional:true# Example
@@ -173,7 +173,7 @@ end
173
173
# app/models/message.rb
174
174
classMessage < ApplicationRecord
175
175
# Provides methods like tool_call?, tool_result?
176
-
acts_as_message #Assumes Chat and ToolCall model names
176
+
acts_as_message #Defaults to Chat and ToolCall model names
177
177
178
178
# --- Add your standard Rails model logic below ---
179
179
# Note: Do NOT add "validates :content, presence: true"
@@ -187,7 +187,7 @@ end
187
187
# app/models/tool_call.rb (Only if using tools)
188
188
classToolCall < ApplicationRecord
189
189
# Sets up associations to the calling message and the result message.
190
-
acts_as_tool_call #Assumes Message model name
190
+
acts_as_tool_call #Defaults to Message model name
191
191
192
192
# --- Add your standard Rails model logic below ---
193
193
end
@@ -496,6 +496,45 @@ This setup allows for:
496
496
497
497
Your `Chat`, `Message`, and `ToolCall` models are standard ActiveRecord models. You can add any other associations, validations, scopes, callbacks, or methods as needed for your application logic. The `acts_as` helpers provide the core persistence bridge to RubyLLM without interfering with other model behavior.
498
498
499
+
You can use custom model names by passing parameters to the `acts_as` helpers. For example, if you prefer `Conversation` over `Chat`, you could use `acts_as_chat` in your `Conversation` model and then specify `chat_class: 'Conversation'` in your `Message` model's `acts_as_message` call.
500
+
501
+
### Using Custom Model Names
502
+
503
+
If your application uses different model names, you can configure the `acts_as` helpers accordingly:
504
+
505
+
```ruby
506
+
# app/models/conversation.rb (instead of Chat)
507
+
classConversation < ApplicationRecord
508
+
# Specify custom model names if needed (not required if your models
0 commit comments