I have Tables
我有桌子
Order
订购
+-----+----------+------+-----------+------------+
| id | id_reff | pay0 | last_pay | date |
+-----+----------+------+-----------+------------+
| 9 | 0110123 | 7 | 6 | 2017-10-20 |
| 8 | 0110123 | 6 | 5 | 2017-10-19 |
| 6 | 0110123 | 5 | 4 | 2017-10-15 |
| 5 | 0110123 | 4 | 3 | 2017-10-12 |
| 3 | 0110123 | 3 | 2 | 2017-10-08 |
| 2 | 0110123 | 2 | 1 | 2017-10-01 |
+-----+----------+------+-----------+------------+
customer
顾客
+----+---------+------+
| id | id_reff| name |
+----+---------+------+
| 1 | 110123 | jon | <<<
| 2 | 110124 | jin |
| 3 | 110125 | jun |
| 4 | 110126 | jan |
| 5 | 110127 | jack |
| 6 | 110128 | jick |
+----+---------+------+
Controller : customers_controller.rb
控制器:customers_controller.rb
def get_history
render json: {
customer: Customer.find_by(id_reff: params[:id_reff]),
bill: Order.where(id_reff: params[:id_reff]).last
end
Order _Form.rb
订单_Form.rb
pay0 :
last_pay : <get from previous pay0>
etc
etc
issue : if i create new OR edit any Order , action .last give me this
问题:如果我创建新的OR编辑任何订单,动作.last给我这个
customer: | 1 | 110123 | jon |
bill: | 9 | 0110123 | 7 | 6 | 2017-10-20 |
=========
=========
if i edit : order[id: 8][date: 2017-10-19]
i should get
>> order.date = 2017-10-15 ; order.pay0 = 5
or
if i edit order[id: 5][date: 2017-10-12]
i should get
>> order.date = 2017-10-08 ; order.pay0 = 3
=========
=========
please help me to rebuild get_history action in customers_controller.rb
请帮我在customers_controller.rb中重建get_history操作
rails 4.2.3
铁轨4.2.3
ruby 2.3.3
红宝石2.3.3
2 个解决方案
#1
1
When you invoke .last
it will return the record with the higher id.
当你调用.last时,它将返回具有更高id的记录。
In your specific case, you could add a custom scope to your Order
model such as:
在您的特定情况下,您可以为订单模型添加自定义范围,例如:
class Order < ActiveRecord::Base
# ...
scope :most_recent, ->() { order(date: :desc).first }
end
The only change you will have to implement is:
您必须实现的唯一变化是:
def get_history
render json: {
customer: Customer.find_by(id_reff: params[:id_reff]),
bill: Order.where(id_reff: params[:id_reff]).most_recent
end
Note that this solution would working assuming that you will have only one order per id_reff
per date
请注意,此解决方案可以正常运行,假设每个日期每个id_reff只有一个订单
For further explanation, try to have a look at scopes in Rails Documentation
有关进一步说明,请尝试查看Rails文档中的范围
#2
0
if i edit : order[id: 8][date: 2017-10-19]
如果我编辑:订单[id:8] [日期:2017-10-19]
Assuming that you have access to order
's id
and date
, you can have a scope like this
假设您可以访问订单的ID和日期,您可以拥有这样的范围
scope :previous_order, -> { order(date: :desc).first }
Then you can call it in your controller
然后你可以在你的控制器中调用它
Order.where('date <= ? and id != ?', params[:date], params[:id]).previous_order
#1
1
When you invoke .last
it will return the record with the higher id.
当你调用.last时,它将返回具有更高id的记录。
In your specific case, you could add a custom scope to your Order
model such as:
在您的特定情况下,您可以为订单模型添加自定义范围,例如:
class Order < ActiveRecord::Base
# ...
scope :most_recent, ->() { order(date: :desc).first }
end
The only change you will have to implement is:
您必须实现的唯一变化是:
def get_history
render json: {
customer: Customer.find_by(id_reff: params[:id_reff]),
bill: Order.where(id_reff: params[:id_reff]).most_recent
end
Note that this solution would working assuming that you will have only one order per id_reff
per date
请注意,此解决方案可以正常运行,假设每个日期每个id_reff只有一个订单
For further explanation, try to have a look at scopes in Rails Documentation
有关进一步说明,请尝试查看Rails文档中的范围
#2
0
if i edit : order[id: 8][date: 2017-10-19]
如果我编辑:订单[id:8] [日期:2017-10-19]
Assuming that you have access to order
's id
and date
, you can have a scope like this
假设您可以访问订单的ID和日期,您可以拥有这样的范围
scope :previous_order, -> { order(date: :desc).first }
Then you can call it in your controller
然后你可以在你的控制器中调用它
Order.where('date <= ? and id != ?', params[:date], params[:id]).previous_order