Class: RcrewAI::Rails::Tools::ActiveRecordTool
- Inherits:
-
RCrewAI::Tools::Base
- Object
- RCrewAI::Tools::Base
- RcrewAI::Rails::Tools::ActiveRecordTool
- Defined in:
- lib/rcrewai/rails/tools/active_record_tool.rb
Instance Method Summary collapse
- #execute(query_type:, model: nil, conditions: {}) ⇒ Object
-
#initialize(model_class: nil, allowed_methods: %i[find where count])) ⇒ ActiveRecordTool
constructor
A new instance of ActiveRecordTool.
Constructor Details
#initialize(model_class: nil, allowed_methods: %i[find where count])) ⇒ ActiveRecordTool
Returns a new instance of ActiveRecordTool.
16 17 18 19 20 |
# File 'lib/rcrewai/rails/tools/active_record_tool.rb', line 16 def initialize(model_class: nil, allowed_methods: %i[find where count]) super() @model_class = model_class @allowed_methods = allowed_methods.map(&:to_sym) end |
Instance Method Details
#execute(query_type:, model: nil, conditions: {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rcrewai/rails/tools/active_record_tool.rb', line 22 def execute(query_type:, model: nil, conditions: {}) query_type = query_type.to_sym validate_query_type!(query_type) model_klass = resolve_model_class(model) conds = (conditions || {}).transform_keys(&:to_sym) case query_type when :find model_klass.find(conds[:id]) when :find_by model_klass.find_by(conds) when :where model_klass.where(conds) when :count conds.empty? ? model_klass.count : model_klass.where(conds).count when :pluck field = conds.delete(:field) model_klass.where(conds).pluck(field) when :exists? model_klass.exists?(conds) when :first model_klass.where(conds).first when :last model_klass.where(conds).last when :all model_klass.where(conds).to_a else raise ArgumentError, "Unsupported query type: #{query_type}" end rescue ActiveRecord::RecordNotFound => e { error: "Record not found", message: e. } rescue => e { error: "Query failed", message: e. } end |