我正在为我的应用程序中的“玩家”做一个过滤器部分..
我此时按“位置”过滤,但我需要在数据库中过滤“birthday.year”生日已完成1900-00-00
我以前做过很好的研究,但我可以“混合”或“结合”我的参数...我找到的最佳答案就在这里(所以它不是重复的) Rails: combining optional params into a query
我是Rails中的菜鸟,所以我会感谢任何帮助我只是做一个设计的整合..
这是我的代码,但我如何使用minyear和maxyear按位置和年龄进行过滤,例如..
谢谢!
def index
@candidates = Player.order("created_at DESC")
position = params[:position]
minyear = params[:minyear]
maxyear = params[:maxyear]
if position == 'goalkeeper'
@candidates = @candidates.where(position:'goalkeeper')
elsif position == 'cedefense'
@candidates = @candidates.where(position:'cedefense')
elsif position == 'ridefense'
@candidates = @candidates.where(position:'ridefense')
elsif position == 'ledefense'
@candidates = @candidates.where(position:'ledefense')
elsif position == 'defmedium'
@candidates = @candidates.where(position:'defmedium')
elsif position == 'ofemedium'
@candidates = @candidates.where(position:'ofemedium')
elsif position == 'rimedium'
@candidates = @candidates.where(position:'rimedium')
elsif position == 'lemedium'
@candidates = @candidates.where(position:'lemedium')
elsif position == 'offensive'
@candidates = @candidates.where(position:'offensive')
elsif position == 'scoach'
@candidates = @candidates.where(position:'scoach')
elsif position == 'sprepf'
@candidates = @candidates.where(position:'sprepf')
else
@candidates = Player.all
end
经过大量研究后,我想出了这个
position = params[:position]
minyear = params[:minyear]
maxyear = params[:maxyear]
if params[:position].nil?
@candidates = Player.all
elsif !params[:position].nil? && params[:minyear].nil?
@candidates = @candidates.where("position = ?", position)
elsif !params[:minyear].nil?
@candidates = @candidates.where("position = ? and birthday = ?", position, minyear )
else
@candidates = Player.all
end
现在唯一的问题是,我之前说过的生日有一个完整的格式,我只对这一年感兴趣......我怎么能解决这个问题?
提前致谢
没关系,它可以像这样工作
@candidates = @candidates.where("position = ? and birthday < ?", position, minyear )
感谢Alex D我现在就这样,
@candidates = Player.scoped # for Rails 3
if params[:position].present?
@candidates = @candidates.where(position: position)
end
if year = params[:year]
date = Date.new(year)
# this will find all people whose birthday is within the given year
# using YEAR(birthday) will likely cause a full table scan;
# it's better to use a range query
@candidates = @candidates.where("birthday >= ? AND birthday <= ?", Date.new(minyear), Date.new(maxyear).end_of_year)
end
答案 0 :(得分:1)
首先,如果您知道Active Record的where
和其他类似方法是可链接的,您可以改进代码:
@candidates = Player.order('created_at DESC')
# if you don't want to set a default order, you can use Player.scoped in Rails 3
# I forget what it is for Rails 4. Maybe just Player.all.
# In Rails 3, .all returns an Array, which doesn't allow you to chain additional
# where conditions, etc.
if params[:position].present?
@candidates = @candidates.where(position: position)
end
if params[:minyear].present?
@candidates = @candidates.where(birthday: minyear)
end
现在第二部分:你真的想在生日那一年匹配。有几种方法可以做到这一点,但如果您有正确的索引,这可以从数据库索引中受益:
if year = params[:year]
date = Date.new(year)
# this will find all people whose birthday is within the given year
# using YEAR(birthday) will likely cause a full table scan;
# it's better to use a range query
@candidates = @candidates.where("birthday >= ? AND birthday <= ?", date, date.end_of_year)
end
由于你的param被称为minyear
,我猜你实际上可能想要所有生日都在给定年份或更晚的人。在那种情况下:
@candidates = @candidates.where("birthday >= ?", Date.new(year))
或者如果它是minyear
和maxyear
:
@candidates = @candidates.where("birthday >= ? AND birthday <= ?", Date.new(minyear), Date.new(maxyear).end_of_year)
答案 1 :(得分:1)
@candidates = @candidates.where("position = ? and year(birthday) < ?", position, minyear )
尝试一下,这将符合您的目的。