0

I'm using a find_by_sql method to search users in my userstable.

is there a possibility to use rails code in the select statement?

 User.find_by_sql ["SELECT DISTINCT
                    users.*
                  FROM
                    users
                  JOIN
                    clients_courses cc
                  ON
                    cc.client_id = users.client_id
                  LEFT JOIN
                    memberships m
                  ON
                    m.user_id = users.id AND m.course_id = cc.course_id
                  WHERE
                    cc.course_id = ?
                    AND
                    m.user_id IS NULL
                    AND
                    users.active = ?
                    AND
                    users.firstname LIKE ? or users.lastname LIKE ?
                    AND NOT IN ( RAILS CODE )", self.id, true, "#{search}%", "#{search}%"]
  end

I Marked the position with RAILS CODE

I want to do someting linke this:

Membership.where("course_id = ?", self.id).users

is there a way to do this?

4
  • Why are you using a select_by_sql anyway? This can be done with activerecord uniq, join and where methods Commented Dec 11, 2015 at 15:05
  • can you show me how it works? Commented Dec 11, 2015 at 15:22
  • Do you need ability to select users for multiple mamberships or just for single id each time? Commented Dec 11, 2015 at 15:47
  • yes multiple memberships Commented Dec 12, 2015 at 10:02

1 Answer 1

1

You can do this -

    member_user_ids  = []
    Membership.where("course_id = ?", self.id).map{|membership| membership.users.map{|user| member_user_ids << user.id}}

    # you might want to put a uniq! on member_user_ids

    User.find_by_sql ["SELECT DISTINCT
                users.*
              FROM
                users
              JOIN
                clients_courses cc
              ON
                cc.client_id = users.client_id
              LEFT JOIN
                memberships m
              ON
                m.user_id = users.id AND m.course_id = cc.course_id
              WHERE
                cc.course_id = ?
                AND
                m.user_id IS NULL
                AND
                users.active = ?
                AND
                users.firstname LIKE ? or users.lastname LIKE ?
                AND users.id NOT IN ( #{member_user_ids.join(',')} )", self.id, true, "#{search}%", "#{search}%"]

You can also have a look at link which explains how to put array of strings in where clause.

Sign up to request clarification or add additional context in comments.

6 Comments

In dev mode it works perfectly. But in test mode It runs into an error: undefined method `users' for #<Membership::ActiveRecord_Relation:0x007fa0119afe08> What could be the reason
Thanks for update, but I got this error again: undefined method `users' for #<Membership:0x00000007984110>
is your db migrated properly for test environment ? and data available in them.
Can you post your model files, for your associations. If Membership belongs_to user, then you would need to use membership.user.id and push it into the array variable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.