Skip to content

docs: add guide for using custom model names with acts_as helpers #171

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

Merged
merged 5 commits into from
Jul 21, 2025
Merged
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
45 changes: 42 additions & 3 deletions docs/guides/rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Include the RubyLLM helpers in your ActiveRecord models:
class Chat < ApplicationRecord
# Includes methods like ask, with_tool, with_instructions, etc.
# Automatically persists associated messages and tool calls.
acts_as_chat # Assumes Message and ToolCall model names
acts_as_chat # Defaults to Message and ToolCall model names

# --- Add your standard Rails model logic below ---
belongs_to :user, optional: true # Example
Expand All @@ -173,7 +173,7 @@ end
# app/models/message.rb
class Message < ApplicationRecord
# Provides methods like tool_call?, tool_result?
acts_as_message # Assumes Chat and ToolCall model names
acts_as_message # Defaults to Chat and ToolCall model names

# --- Add your standard Rails model logic below ---
# Note: Do NOT add "validates :content, presence: true"
Expand All @@ -187,7 +187,7 @@ end
# app/models/tool_call.rb (Only if using tools)
class ToolCall < ApplicationRecord
# Sets up associations to the calling message and the result message.
acts_as_tool_call # Assumes Message model name
acts_as_tool_call # Defaults to Message model name

# --- Add your standard Rails model logic below ---
end
Expand Down Expand Up @@ -496,6 +496,45 @@ This setup allows for:

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.

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.

### Using Custom Model Names

If your application uses different model names, you can configure the `acts_as` helpers accordingly:

```ruby
# app/models/conversation.rb (instead of Chat)
class Conversation < ApplicationRecord
# Specify custom model names if needed (not required if your models
# are called Message and ToolCall)
acts_as_chat message_class: 'ChatMessage', tool_call_class: 'AIToolCall'

belongs_to :user, optional: true
# ... your custom logic
end

# app/models/chat_message.rb (instead of Message)
class ChatMessage < ApplicationRecord
# Let RubyLLM know to use your Conversation model instead of the default Chat
acts_as_message chat_class: 'Conversation', tool_call_class: 'AIToolCall'
# You can also customize foreign keys if needed:
# chat_foreign_key: 'conversation_id'

# ... your custom logic
end

# app/models/ai_tool_call.rb (instead of ToolCall)
class AIToolCall < ApplicationRecord
acts_as_tool_call message_class: 'ChatMessage'
# Optionally customize foreign keys:
# message_foreign_key: 'chat_message_id'

# ... your custom logic
end
```

This flexibility allows you to integrate RubyLLM with existing Rails applications that may already have naming conventions established.

Some common customizations include:

```ruby
Expand Down