用途:该技能面向 Rails 的 ActiveRecord,用于回答关联、数据库查询、验证、迁移、作用域、回调、N+1 等问题,并给出数据库优化指导。
主要能力: 关联:belongs_to、has_one、has_many、has_many :through、has_one :through、has_and_belongs_to_many,以及多态、自引用关联;支持 dependent、optional、inverse_of、counter_cache、touch、class_name、foreign_key 等选项。 验证:presence、absence、acceptance、confirmation、uniqueness、format、length、numericality、inclusion、exclusion、comparison、validates_associated;支持 :message、:on、:allow_nil、:allow_blank、:if/:unless、:strict 及自定义验证。 查询:find、find_by、where、not、or、select、distinct、pluck、ids、exists?、count/sum/average、order、limit、offset、参数化 SQL、子查询、group/having。 作用域:scope 定义可链式查询,支持参数与默认值。 预加载:includes、preload、eager_load、joins 的差异与 N+1 预防。 回调:before_validation、before_save、after_create、after_commit 等及最佳实践。 迁移:create_table、add_index、并发索引、find_each/in_batches 批处理。
使用方式:作为技能在用户询问 ActiveRecord 相关问题时调用,按主题提供示例与建议。
适用场景:Rails 应用的数据建模、查询编写、验证设计、性能优化与迁移编写。
限制:资料未提供安装命令、收费、价格、平台或效果信息;示例基于 Rails 与 ActiveRecord,具体版本行为需以实际环境为准。