Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

self-referential association #5

Open
peterwongpp opened this issue Apr 28, 2014 · 1 comment
Open

self-referential association #5

peterwongpp opened this issue Apr 28, 2014 · 1 comment

Comments

@peterwongpp
Copy link

Not sure if I did not set it up correctly. But I failed to get the expected result set from self-referential association.

I have a Category model, which has many sub-categories:

class Category < ActiveRecord::Base
  belongs_to :parent_category, class_name: :Category
  has_many :child_categories, class_name: :Category, foreign_key: "parent_category_id"
end

Then in my filter class:

class CategoriesFilter < FilterMe::ActiveRecordFilter
  model Category
  association :parent_category, filter_class: CategoriesFilter

  field :name, [:eq]
end

Then when I do the filter with the following filter params:

{ filters: { parent_category: { name: { eq: "Category 1" } } } }

But this generates the following SQL query:

SELECT "categories".* FROM "categories"
    INNER JOIN "categories" "parent_categories_categories"
    ON "parent_categories_categories"."id" = "categories"."parent_category_id"
    WHERE "categories"."name" = 'Category 1'

Guess the WHERE clause should be "parent_categories_categories"."name" instead of "categories"."name".

Any idea?

@Samsinite
Copy link
Owner

Hi Peter,

Thanks for filing the issue. I'm excited to hear that someone else is using -- or attempting to from the appearance of it -- the filtering gem. Let me put together a test case for your issue and get it fixed.

@Samsinite Samsinite added the bug label Apr 28, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants