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

Filter by existence of many-to-many association example #71

Open
antijedi opened this issue Nov 30, 2015 · 1 comment
Open

Filter by existence of many-to-many association example #71

antijedi opened this issue Nov 30, 2015 · 1 comment

Comments

@antijedi
Copy link

For me the example only worked when i added .project(Arel.star) before .exists, so it would look like this:

scope :with_any_role_ids, lambda{ |role_ids|
  # get a reference to the join table
  role_assignments = RoleAssignment.arel_table
  # get a reference to the filtered table
  students = Student.arel_table
  # let AREL generate a complex SQL query
  where(
    RoleAssignment \
      .where(role_assignments[:student_id].eq(students[:id])) \
      .where(role_assignments[:role_id].in([*role_ids].map(&:to_i))) \
      .project(Arel.star) \
      .exists
  )
}

Not sure if it's only in my usecase, but without it I got sql syntax error.

@jhund
Copy link
Owner

jhund commented Dec 1, 2015

@antijedi thank you for reporting the issue. What version of Rails/ActiveRecord are you using?

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

No branches or pull requests

2 participants