Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with ransackable_attribute not handling symbols correctly #1539

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tatematsu-k
Copy link

@tatematsu-k tatematsu-k commented Nov 21, 2024

Fix: #1538

If the behavior is as intended, please close this PR.


Problem

When using the ransackable_attributes method in ActiveRecord, passing an array of symbols causes the search conditions for the corresponding attributes to be ignored. However, passing an array of strings works as expected.

Steps to Reproduce

1: Create a migration for a users table with a name column:

class CreateUsers < ActiveRecord::Migration[7.1]
  def change
    create_table :users do |t|
      t.string :name

      t.timestamps
    end
  end
end

2: Define the User model with ransackable_attributes returning a symbol array:

class User < ApplicationRecord
  def self.ransackable_attributes(auth_object = nil)
    [:name]
  end
end

3: Attempt to perform a Ransack query with a condition on the name attribute:

User.ransack(name_cont: "hoge").result.to_sql

Actual Behavior

The generated SQL query ignores the condition for name:

SELECT "users".* FROM "users"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug?: ransackable_attributes Fails with Symbol Array
1 participant