Morez SA Morez SA - 1 year ago 93
Ruby Question

Find attribute in rails controller

I'm a bit of a Ruby on Rails newbie, I am trying to find the last record for a specific column in the database. This system accepts Bitcoins and generates and saves a Bitcoin address and later an amount is also saved.

I created the following migration:

def change
create_table :payments do |t|
t.string :bitcoin
t.string :amount


in the controller I have:

class PaymentsController < ApplicationController

def new_address
@balance = BlockIo.get_new_address

I want to get the last saved Bitcoin address. I have tried:

payment = Payment.find(params[:id])
last_payment = payment.bitcoin.last

I also tried:

payment = Payment.find(params[:id])
last_payment = payment.pluck(:bitcoin).last

And a few other combinations as I checked through Active Record documentation. Please assist me with the correct syntax to get the last saved bitcoin address.

In the show view, you can access the data via:

Please advise how to check last saved bitcoin address based on the above information. Any assistance will be greatly appreciated.

Answer Source

Try simply

last_payment = Payment.last.bitcoin
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download