Workivo
Menu

Learn Ruby

Learn Ruby

This guide helps you write business rules in Ruby. Ruby is a readable, expressive language — these resources will get you started.

Interactive tutorials

Reference docs

Common patterns for rules

Working with strings

record.name = record.name.strip
record.name = record.name.upcase
record.name = record.name.downcase
record.name = record.name.capitalize
record.name = "prefix_#{record.name}"

Working with dates and times

now = Time.current
today = Date.current
record.due_date = 7.days.from_now
record.completed_at = Time.current if record.status == "done"

Checking conditions

if record.name.blank?
  raise Rules::RuleError, "Name is required"
end

if record.email.present? && record.email.include?("@")
  record.domain = record.email.split("@").last
end

Fetching external data

require "net/http"
uri = URI("https://example.com")
resp = Net::HTTP.get_response(uri)
record.http_status = resp.code

Creating audit logs

Log.create!(
  source: "rule",
  message: "Updated record #{record.id}",
  context: { actor: actor&.username }.to_json
)

Tips

  • Each rule runs in its own scope — local variables don't leak
  • Use raise Rules::RuleError to abort the action with a message
  • In before_* stages, modifying the record changes what gets saved
  • In after_* stages, the record is already saved — use side effects
  • Check actor to know who triggered the action
  • Use attributes to see what form values were submitted

Back:

Please confirm

Are you sure?