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
> 
> 
> 

```
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
> 
> 
> 

```
pay0 :
last_pay : <get from previous pay0>
etc
etc

```

**issue :**
if i create **new** OR **edit** any **Order** , action .last give me this

```
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
> 
> 
> rails 4.2.3
> 
> 
> ruby 2.3.3
> 
> 
>