gpt4 book ai didi

mysql - 通过查询搜索时包括父属性

转载 作者:行者123 更新时间:2023-11-29 05:52:15 25 4
gpt4 key购买 nike

我有一个用户模型,其中包含_许多职位申请。

一切正常,但当我尝试按用户的名字搜索职位申请时,出现以下错误。

错误

SQLite3::SQLException: no such column: first_name: SELECT  "job_applications"

据我了解,我需要在求职申请查询中包含用户属性。

我怎样才能做到这一点?

查看(工作申请)

<%= form_for :search, :html => {:method => :get, :id => 'search'} do |f| %>
<%= text_field_tag :terms, params[:terms], :type => 'search' %>
<% end %>

控制者(工作申请)

def index
@job = Job.find(params[:job_id])
@job_applications = @job.job_applications.search(params[:terms])
end

模特(求职)

def self.search(terms)
terms ||= ""
conditions = terms.split(" ").map do |term|
term = term.strip.gsub("'","''")

### I am having an issue here...
### how do i include the user attributes in this query
"first_name like '%#{term}%'"
end
where(conditions.join " OR ")
end

最佳答案

你必须加入 job_applications 表和 users 表。

# job_applications.rb

def self.search(terms)
terms ||= ""
conditions = terms.split(" ").map do |term|
term = term.strip.gsub("'","''")
"users.first_name like :term"
end
joins(:user).where(conditions.join " OR ")
end

避免将原始用户的输入直接传递到您的查询中,以避免 sql 注入(inject)。使用 Rails 的内置过滤器或自行清理。

def self.search(terms)
terms ||= ""
term_args = []
conditions = terms.split(" ").map do |term|
term = term.strip.gsub("'","''")
term_args << "%#{term}%"
"users.first_name like ?"
end
joins(:user).where(conditions.join(' OR '), term_args)
end

关于mysql - 通过查询搜索时包括父属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52912028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com