Skip to content

Commit c87f6c3

Browse files
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>
1 parent 8e056dc commit c87f6c3

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

docs/guides/rails.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ Include the RubyLLM helpers in your ActiveRecord models:
163163
class Chat < ApplicationRecord
164164
# Includes methods like ask, with_tool, with_instructions, etc.
165165
# 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
167167

168168
# --- Add your standard Rails model logic below ---
169169
belongs_to :user, optional: true # Example
@@ -173,7 +173,7 @@ end
173173
# app/models/message.rb
174174
class Message < ApplicationRecord
175175
# 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
177177

178178
# --- Add your standard Rails model logic below ---
179179
# Note: Do NOT add "validates :content, presence: true"
@@ -187,7 +187,7 @@ end
187187
# app/models/tool_call.rb (Only if using tools)
188188
class ToolCall < ApplicationRecord
189189
# 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
191191

192192
# --- Add your standard Rails model logic below ---
193193
end
@@ -496,6 +496,45 @@ This setup allows for:
496496

497497
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.
498498

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+
class Conversation < ApplicationRecord
508+
# Specify custom model names if needed (not required if your models
509+
# are called Message and ToolCall)
510+
acts_as_chat message_class: 'ChatMessage', tool_call_class: 'AIToolCall'
511+
512+
belongs_to :user, optional: true
513+
# ... your custom logic
514+
end
515+
516+
# app/models/chat_message.rb (instead of Message)
517+
class ChatMessage < ApplicationRecord
518+
# Let RubyLLM know to use your Conversation model instead of the default Chat
519+
acts_as_message chat_class: 'Conversation', tool_call_class: 'AIToolCall'
520+
# You can also customize foreign keys if needed:
521+
# chat_foreign_key: 'conversation_id'
522+
523+
# ... your custom logic
524+
end
525+
526+
# app/models/ai_tool_call.rb (instead of ToolCall)
527+
class AIToolCall < ApplicationRecord
528+
acts_as_tool_call message_class: 'ChatMessage'
529+
# Optionally customize foreign keys:
530+
# message_foreign_key: 'chat_message_id'
531+
532+
# ... your custom logic
533+
end
534+
```
535+
536+
This flexibility allows you to integrate RubyLLM with existing Rails applications that may already have naming conventions established.
537+
499538
Some common customizations include:
500539

501540
```ruby

0 commit comments

Comments
 (0)