在我的数据库中,我有两个名为start_date
and的列,如果今天的日期在andend_date
之间,我想从我的数据库中获取条目。我当前的查询代码如下,但它没有返回我所期望的:start_date
end_date
def updateMess
@mess = MessType.find(params[:id_param])
@today = Date.today
@mess_current_price = MessPrice.where('mess_id = ? AND DATE(?) BETWEEN start_date AND end_date', @mess.mess_id, @today)
respond_to do |format|
format.json { render :json => { :_mess => @mess, :_mess_price => @mess_current_price } }
end
end
上面的where
命令生成以下查询(如控制台所示):
MessPrice Load (0.4ms) SELECT
mess_prices
.* FROMmess_prices
WHERE (DATE('2013-07-01') BETWEEN start_date AND end_date)在 17 毫秒内完成 200 次 OK(查看次数:3.1 毫秒 | ActiveRecord:2.7 毫秒)
编辑:以下是我的 javascript 代码,我使用解析的 JSON 数据在我的视图中设置字段,但它没有在我的视图中设置字段:
$(document).on('click', ".mess-update-reveal", function(){
$.post('/mess/updateMess',{id_param :$(this).attr('data-mess-id')}, function(data){
var mess = data._mess;
var messPrice = data._mess_price;
console.log(data);
$('#id_param_update_mess').val(mess.mess_id);
$('#mess_name_update').val(mess.mess_name);
$('#id_mess_charge_update').val(messPrice.id);
$('#mess_breakfast_charge_update').val(messPrice.breakfast_charge);
$('#mess_lunch_charge_update').val(messPrice.lunch_charge);
$('#mess_dinner_charge_update').val(messPrice.dinner_charge);
$('#mess_monthly_charge_update').val(messPrice.monthly_charge);
$('#mess_semesterly_charge_update').val(messPrice.semesterly_charge);
$('#mess_capacity_update').val(mess.mess_capacity);
$('#start_date_update').val(messPrice.start_date);
$('#end_date_update').val(messPrice.end_date);
$('#rebate_update').val(messPrice.rebate);
});
});
显示的结果console.log(data)
如下:
Object
_mess: Object
created_at: "2013-06-26T07:16:58Z"
hostel_id: null
mess_capacity: 200
mess_id: 1
mess_name: "Mess1"
updated_at: "2013-06-29T12:08:08Z"
__proto__: Object
_mess_price: Array[1]
0: Object
breakfast_charge: 30
created_at: null
dinner_charge: 45
end_date: "2013-12-31"
id: 1
lunch_charge: 45
mess_id: 1
monthly_charge: 2000
rebate: 100
semesterly_charge: 11000
start_date: "2013-06-01"
updated_at: "2013-06-29T12:38:51Z"
__proto__: Object
length: 1
__proto__: Array[0]
__proto__: Object