stonep
stonep

Reputation: 309

Rails / Scope search on joined table

Attempt to setup a lightweight search engine, having no problems setting up scopes on the model with it's own attributes. Having some roadblocks on getting a joined table to play nice

       scope :seniority, -> (seniority) { where seniority: seniority }

       # merged scopes
       scope :edu, -> joins(:candidate_schools).where("candidate_schools.degree = #{edu}") }

Seniority works fine as it is a normal attribute. My ultimate goal is to render user objects back from an associate table such as

     http://localhost:3002/api/v1/users?sector=tech&seniority=jr&edu=master

Where passing params for "edu", would be searching the "candidate_schools" table and filtering based on the "degree" attribute, if the edu params match a record it would process a "user" object

Upvotes: 0

Views: 804

Answers (1)

user419017
user419017

Reputation:

Change your scope to this:

scope :edu, -> (edu) {
  joins(:candidate_schools).where("candidate_schools.degree = ?", edu) 
}

# usage
User.seniority("jr").edu("master")

Don't do "candidate_schools.degree = #{edu}", look into sql injection for why.

Upvotes: 1

Related Questions