SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • ShellShell
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
Ruby

Ruby static code analysis

Unique rules to find Bugs, Security Hotspots, and Code Smells in your RUBY code

  • All rules 75
  • Bug17
  • Security Hotspot2
  • Code Smell56
Filtered: 2 rules found
activerecord
    Impact
      Clean code attribute
        1. Rails queries should use "find_by" instead of "where.take" for single record retrieval

           Code Smell
        2. ActiveRecord models should override "as_json" instead of "to_json" for custom JSON serialization

           Bug

        ActiveRecord models should override "as_json" instead of "to_json" for custom JSON serialization

        consistency - conventional
        reliability
        maintainability
        Bug
        • rails
        • activerecord
        • json
        • serialization

        This is an issue when an ActiveRecord model overrides the to_json method for custom JSON serialization.

        Why is this an issue?

        How can I fix it?

        More Info

        Rails uses a two-step process for JSON serialization. First, as_json converts the object to a hash representation. Then, to_json converts that hash to a JSON string.

        When you override to_json in an ActiveRecord model, you break Rails' JSON rendering pipeline. The render json: method in controllers expects to call as_json on your model, but if you’ve overridden to_json, this can lead to several problems:

        • ArgumentError exceptions: The to_json method signature may not match what Rails expects, causing "wrong number of arguments" errors.
        • Broken controller rendering: Using render json: @model may not work as expected because Rails calls as_json internally, not your custom to_json method.
        • Inconsistent behavior: Your custom serialization will only work when to_json is called directly, not through Rails' standard JSON rendering.

        The as_json method is specifically designed to be overridden. It receives an options hash that you can merge with your custom options, making it flexible and compatible with Rails' rendering system.

        What is the potential impact?

        Overriding to_json instead of as_json can cause runtime errors in your Rails application, particularly ArgumentError exceptions when using render json: in controllers. This can lead to broken API endpoints and inconsistent JSON serialization behavior across your application.

          Available In:
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use