Token导航 LogoToken导航

Agent Skill · 研究检索

rails-active-record-patterns(Rails active record 模式)

rails-active-record-patterns 用于查找、检索和筛选相关信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要根据关键词、任务场景或来源线索快速定位候选结果时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

rails-active-record-patterns 是研究检索类 Agent Skill,可用宿主为 Codex、Claude、Cursor、Gemini CLI,通过GitHub 仓库获取,权限为需要联网;许可证为 MIT,第三方扫描有未通过项,按安装量在研究检索类的 73,675 个 Skill 里排第 46,467。

总安装 776周安装 33GitHub Stars 142下载 272核验 2026-05-01

安装说明

本站只整理中文说明,不托管安装包,也不代用户安装。
GitHub
许可证
MIT

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:rails-active-record-patterns(Rails active record 模式)
来源仓库:https://github.com/thebushidocollective/han
仓库路径:skills/rails-active-record-patterns
安装命令:
npx skills add https://github.com/thebushidocollective/han --skill rails-active-record-patterns
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。不同来源提供的安装方式可能略有差异;本站展示可直接复制的安装命令,安装前请核对来源页面。

skills.shnpx skills
npx skills add https://github.com/thebushidocollective/han --skill rails-active-record-patterns

这个 Skill 是做什么的

研究检索类 Skill 主要解决「找资料、核对事实、把来源整理清楚」的问题,通常会让助手先检索再总结。

用于检索 Rails 中 Active Record 的数据建模与查询优化方案。适合在数据库设计或性能调优时寻找典型用法与反模式。通过 GitHub 安装,支持按关键词搜索相关代码片段或指南。应避免直接复制未经测试的查询语句,尤其涉及关联与事务时。推荐结合 EXPLAIN 分析慢查询,确保执行效率。

来源仓库内路径:skills/rails-active-record-patterns

上手步骤

4 步
  1. 1

    确认自己的宿主工具

    公开资料标注支持 Codex、Claude、Cursor、Gemini CLI;先确认自己用的是其中之一,再按对应工具的 Skills 目录放置。

  2. 2

    拿到安装命令

    可以直接复制的命令是 npx skills add https://github.com/thebushidocollective/han --skill rails-active-…;命令里的占位符换成自己的路径或密钥再执行。

  3. 3

    安装后重启宿主

    命令执行完要完全退出宿主工具再打开,让新装的 Skill 被重新扫描到,否则列表里可能看不到。

  4. 4

    按最小权限试用

    它需要需要联网,先在测试目录或测试账号里跑,确认没问题再放到正式项目。

常见问题

6 条

rails-active-record-patterns 怎么安装?

在终端执行 npx skills add https://github.com/thebushidocollective/han --skill rails-active-record-pat…。装完完全退出宿主工具再打开,让它重新扫描本地 Skill。

rails-active-record-patterns 支持哪些 AI 工具?

公开资料标注支持 Codex、Claude、Cursor、Gemini CLI。其他工具只要能读取本地 Skill 目录,一般也能手动放进去,但要自己确认触发方式是否一致。

rails-active-record-patterns 需要什么权限?

权限级别是需要联网。该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

rails-active-record-patterns 是免费的吗?

来源标注的许可证是 MIT。本站不代收任何费用,也不托管安装包;能否商用、要不要署名,以许可证原文为准。

rails-active-record-patterns 用的人多吗?

公开数据是累计安装 776 次,近一周安装 33 次,下载 272 次,GitHub 142 星。这些数字来自第三方目录统计,只能当作热度参考,不代表质量或安全性。

rails-active-record-patterns 安全吗?

第三方扫描有未通过项,风险较高。除非你确认过源码,否则不建议直接安装到常用环境。

值得选的地方和要先想清楚的地方

值得选的地方

  • 同时适配 4 种工具,换客户端不用重新找替代品。
  • 3 个目录同时收录,说明这个 Skill 流传比较广。
  • 许可证明确为 MIT,商用和分发边界好判断。

先想清楚的地方

  • 第三方扫描存在提醒项或未通过项,需要人工逐条确认。
  • 第三方目录整理,版本更新和行为细节以来源仓库为准。

适不适合你

更适合

  • 已经在用 Codex,想直接扩展它能力的人。
  • 需要处理研究检索类任务、又不想每次都手写提示词的人。

不太适合

  • 需要厂商级技术支持和 SLA 的生产系统。

研究检索类 Skill 对比

6 个同类
Skill总安装GitHub 星权限许可证扫描
find-skills

寻找技能 · Codex、Claude

31,199,81316,486操作浏览器MIT有提醒
self-improving-agent

自我改进 Agent · OpenClaw

16,464,0023,396接触敏感数据MIT-0通过
soultrace

灵魂追踪 · Codex、Claude

7,588,94412需要联网未明确有提醒
self-improving

自我完善 · OpenClaw

6,829,2841,046需要联网MIT-0通过
microsoft-foundry

微软代工厂 · Codex、Claude

6,614,144764调用外部服务未明确有提醒
rails-active-record-patterns

Rails active record 模式 · 本页

776142需要联网MIT未通过

按安装量取同分类前几名;数字来自第三方目录统计,只作热度参考,不代表质量结论。

SKILL.md 原文

Rails Active Record Patterns

Master Active Record patterns for building robust Rails models with proper associations, validations, scopes, and query optimization.

Overview

Active Record is Rails' Object-Relational Mapping (ORM) layer that connects model classes to database tables. It implements the Active Record pattern, where each object instance represents a row in the database and includes both data and behavior.

Installation and Setup

Creating Models

# Generate a model with migrations
rails generate model User name:string email:string:uniq

# Generate model with associations
rails generate model Post title:string body:text user:references

# Run migrations
rails db:migrate

Database Configuration

# config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

Core Patterns

1. Basic Model Definition

# app/models/user.rb
class User < ApplicationRecord
  # Validations
  validates :email, presence: true, uniqueness: true,
    format: { with: URI::MailTo::EMAIL_REGEXP }
  validates :name, presence: true, length: { minimum: 2, maximum: 50 }

  # Callbacks
  before_save :normalize_email
  after_create :send_welcome_email

  # Scopes
  scope :active, -> { where(active: true) }
  scope :recent, -> { order(created_at: :desc).limit(10) }

  private

  def normalize_email
    self.email = email.downcase.strip
  end

  def send_welcome_email
    UserMailer.welcome(self).deliver_later
  end
end

2. Associations

# app/models/user.rb
class User < ApplicationRecord
  # One-to-many
  has_many :posts, dependent: :destroy
  has_many :comments, dependent: :destroy

  # Many-to-many through join table
  has_many :memberships, dependent: :destroy
  has_many :organizations, through: :memberships

  # Has-one
  has_one :profile, dependent: :destroy

  # Polymorphic association
  has_many :images, as: :imageable, dependent: :destroy
end

# app/models/post.rb
class Post < ApplicationRecord
  belongs_to :user
  has_many :comments, dependent: :destroy
  has_many :commenters, through: :comments, source: :user

  # Counter cache
  belongs_to :user, counter_cache: true
end

# app/models/organization.rb
class Organization < ApplicationRecord
  has_many :memberships, dependent: :destroy
  has_many :users, through: :memberships
end

# app/models/membership.rb
class Membership < ApplicationRecord
  belongs_to :user
  belongs_to :organization

  enum role: { member: 0, admin: 1, owner: 2 }
end

3. Advanced Queries

# app/models/post.rb
class Post < ApplicationRecord
  # Scopes with arguments
  scope :by_author, ->(user_id) { where(user_id: user_id) }
  scope :published_after, ->(date) { where('published_at > ?', date) }
  scope :with_tag, ->(tag) { joins(:tags).where(tags: { name: tag }) }

  # Class methods for complex queries
  def self.popular(threshold = 100)
    where('views_count >= ?', threshold)
      .order(views_count: :desc)
  end

  def self.search(query)
    where('title ILIKE ? OR body ILIKE ?', "%#{query}%", "%#{query}%")
  end

  # Query with joins and includes
  def self.with_user_and_comments
    includes(:user, comments: :user)
      .order(created_at: :desc)
  end
end

# Usage
Post.published_after(1.week.ago)
  .by_author(current_user.id)
  .with_tag('rails')
  .popular(50)

4. Validations

# app/models/user.rb
class User < ApplicationRecord
  # Presence validation
  validates :email, :name, presence: true

  # Uniqueness validation
  validates :email, uniqueness: { case_sensitive: false }

  # Format validation
  validates :username, format: {
    with: /\A[a-z0-9_]+\z/,
    message: "only allows lowercase letters, numbers, and underscores"
  }

  # Length validation
  validates :bio, length: { maximum: 500 }
  validates :password, length: { minimum: 8 }, if: :password_required?

  # Numericality validation
  validates :age, numericality: {
    only_integer: true,
    greater_than_or_equal_to: 18,
    less_than: 120
  }

  # Custom validation
  validate :email_domain_allowed

  private

  def email_domain_allowed
    return if email.blank?

    domain = email.split('@').last
    unless ALLOWED_DOMAINS.include?(domain)
      errors.add(:email, "domain #{domain} is not allowed")
    end
  end

  def password_required?
    new_record? || password.present?
  end
end

5. Callbacks

# app/models/post.rb
class Post < ApplicationRecord
  # Before callbacks
  before_validation :normalize_title
  before_save :calculate_reading_time
  before_create :generate_slug

  # After callbacks
  after_create :notify_followers
  after_update :clear_cache, if: :saved_change_to_body?
  after_destroy :cleanup_attachments

  # Around callbacks
  around_save :log_save_time

  private

  def normalize_title
    self.title = title.strip.titleize if title.present?
  end

  def calculate_reading_time
    return unless body_changed?
    words = body.split.size
    self.reading_time = (words / 200.0).ceil
  end

  def generate_slug
    self.slug = title.parameterize
  end

  def notify_followers
    NotifyFollowersJob.perform_later(self)
  end

  def clear_cache
    Rails.cache.delete("post/#{id}")
  end

  def cleanup_attachments
    attachments.purge_later
  end

  def log_save_time
    start = Time.current
    yield
    duration = Time.current - start
    Rails.logger.info "Post #{id} saved in #{duration}s"
  end
end

6. Enum Patterns

# app/models/post.rb
class Post < ApplicationRecord
  # Basic enum
  enum status: {
    draft: 0,
    published: 1,
    archived: 2
  }

  # Enum with prefix/suffix
  enum visibility: {
    public: 0,
    private: 1,
    unlisted: 2
  }, _prefix: :visibility

  # Multiple enums
  enum content_type: {
    article: 0,
    video: 1,
    podcast: 2
  }, _suffix: :content

  # Scopes automatically created
  # Post.draft, Post.published, Post.archived
  # Post.visibility_public, Post.visibility_private
  # Post.article_content, Post.video_content

  # Query methods
  # post.draft?, post.published?, post.archived?
  # post.visibility_public?, post.visibility_private?

  # State transitions
  def publish!
    published! if draft?
  end
end

7. Query Optimization

# app/models/post.rb
class Post < ApplicationRecord
  # Eager loading to avoid N+1
  scope :with_associations, -> {
    includes(:user, :tags, comments: :user)
  }

  # Select specific columns
  scope :title_and_author, -> {
    select('posts.id, posts.title, users.name as author_name')
      .joins(:user)
  }

  # Batch processing
  def self.process_in_batches
    find_each(batch_size: 1000) do |post|
      post.process
    end
  end

  # Pluck for arrays
  def self.recent_titles
    order(created_at: :desc)
      .limit(10)
      .pluck(:title)
  end

  # Exists check (efficient)
  def self.has_recent_posts?(user_id)
    where(user_id: user_id)
      .where('created_at > ?', 1.day.ago)
      .exists?
  end

  # Count with joins
  def self.popular_authors
    joins(:user)
      .group('users.id', 'users.name')
      .select('users.id, users.name, COUNT(posts.id) as posts_count')
      .having('COUNT(posts.id) >= ?', 10)
      .order('posts_count DESC')
  end
end

8. Transactions

# app/services/post_publisher.rb
class PostPublisher
  def self.publish(post, user)
    ActiveRecord::Base.transaction do
      post.update!(status: :published, published_at: Time.current)
      user.increment!(:posts_count)
      NotificationService.notify_followers(post)

      # If any operation fails, entire transaction is rolled back
    end
  rescue ActiveRecord::RecordInvalid => e
    Rails.logger.error "Failed to publish post: #{e.message}"
    false
  end

  # Nested transactions with savepoints
  def self.complex_operation(post)
    ActiveRecord::Base.transaction do
      post.update!(featured: true)

      ActiveRecord::Base.transaction(requires_new: true) do
        # This creates a savepoint
        post.tags.create!(name: 'featured')
      end
    end
  end
end

9. STI (Single Table Inheritance)

# app/models/vehicle.rb
class Vehicle < ApplicationRecord
  validates :make, :model, presence: true

  def max_speed
    raise NotImplementedError
  end
end

# app/models/car.rb
class Car < Vehicle
  validates :doors, presence: true

  def max_speed
    120
  end
end

# app/models/motorcycle.rb
class Motorcycle < Vehicle
  validates :engine_size, presence: true

  def max_speed
    180
  end
end

# Usage
car = Car.create(make: 'Toyota', model: 'Camry', doors: 4)
car.type # => "Car"
Vehicle.all # Returns both cars and motorcycles
Car.all # Returns only cars

10. Concerns

# app/models/concerns/sluggable.rb
module Sluggable
  extend ActiveSupport::Concern

  included do
    before_validation :generate_slug
    validates :slug, presence: true, uniqueness: true
  end

  class_methods do
    def find_by_slug(slug)
      find_by(slug: slug)
    end
  end

  private

  def generate_slug
    return if slug.present?
    base_slug = title.parameterize
    self.slug = unique_slug(base_slug)
  end

  def unique_slug(base_slug)
    slug_candidate = base_slug
    counter = 1

    while self.class.exists?(slug: slug_candidate)
      slug_candidate = "#{base_slug}-#{counter}"
      counter += 1
    end

    slug_candidate
  end
end

# app/models/post.rb
class Post < ApplicationRecord
  include Sluggable
end

Best Practices

  1. Use scopes for reusable queries - Keep query logic in the model
  2. Eager load associations - Prevent N+1 queries with includes/preload
  3. Add database indexes - Index foreign keys and frequently queried columns
  4. Use counter caches - Optimize count queries for associations
  5. Validate at model level - Ensure data integrity with validations
  6. Keep callbacks simple - Extract complex logic to service objects
  7. Use transactions - Ensure data consistency for multi-step operations
  8. Leverage concerns - Share common behavior across models
  9. Use enums for state - Type-safe state management with enums
  10. Write efficient queries - Use select, pluck, and exists appropriately

Common Pitfalls

  1. N+1 queries - Forgetting to eager load associations
  2. Callback hell - Too many callbacks making flow hard to follow
  3. Fat models - Putting too much business logic in models
  4. Missing indexes - Slow queries due to unindexed columns
  5. Unsafe updates - Not using transactions for related operations
  6. Validation bypass - Using update_attribute or save(validate: false)
  7. Memory bloat - Loading all records instead of batching
  8. SQL injection - Using string interpolation in where clauses
  9. Counter cache mismatches - Manual updates breaking counter caches
  10. Ignoring database constraints - Not adding DB-level validations

When to Use

  • Building data-backed Rails applications
  • Implementing business logic tied to database models
  • Creating REST APIs with Rails
  • Developing CRUD interfaces
  • Managing complex data relationships
  • Building multi-tenant applications
  • Creating admin interfaces with Active Admin
  • Implementing soft deletes and audit trails
  • Building reporting and analytics features
  • Creating content management systems

Resources

平台分布

Codex

29.39%
按下载量换算80

Claude Code

21.08%
按下载量换算57

OpenCode

20.3%
按下载量换算55

Gemini CLI

13.24%
按下载量换算36

Cursor

8.93%
按下载量换算24

windsurf

3.5%
按下载量换算10

安全审计

Gen Agent Trust Hub

未通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。来源安全扫描存在提醒或未通过项,安装前请自行复核。

继续浏览同类 Skills