The passed data is ModelObject::ActiveRecord_Relation, not String Rails 5

0 votes
asked Sep 11, 2017 by luis-cardoza-bird

Im trying to obtain the id from a table using a where clause, but the value return as an ActiveRecord_Relation.

def generate_qrcode
    idBuscado = get_id_beneficio(3) #the 3 it's just for testing values
    qrcode = make_qrcode(idBuscado)
    send_data qrcode.to_s, type: 'image/png', disposition: 'inline'
  end


private def get_id_beneficio(idBuscado)
    itemlist = Beneficio.select('Id_beneficio', :conditions => ['id_beneficio = ?', "%#{idBuscado}%"]).limit(1)
  end

private def make_qrcode(cadena = 0)
    qrcode = RQRCode::QRCode.new(cadena)
    image = qrcode.as_png(
        resize_gte_to: false,
        resize_exactly_to: false,
        fill: 'white',
        color: 'red',
        size: 300,
        border_modules: 3,
        module_px_size: 4,
        file: nil # path to write
        )
    image.resize(150, 150)
  end

and the error that show... Error

I need to now how to get the value named Id_beneficio and return it as a Integer. Any advice? :(

[[UPDATE]]

Now with the new suggestion the values that i obtain it's

new error

however in the console show's that the sql command execute smoothly

Beneficio Load (1.0ms)  SELECT  id_beneficio FROM `beneficios` WHERE (id_beneficio = '3') ORDER BY `beneficios`.`Id_beneficio` ASC LIMIT 1

Completed 500 Internal Server Error in 11ms (ActiveRecord: 1.0ms)



RQRCode::QRCodeArgumentError (The passed data is NilClass, not String):

and the new method to obtain the values

private def get_id_beneficio(idBuscado)
    Beneficio.select('id_beneficio')
          .where('id_beneficio = ?', "#{idBuscado}")
          .limit(1)
          .first
          .id
  end

1 Answer

0 votes
answered Sep 11, 2017 by luis-cardoza-bird

Well result that the method i need was

private def get_id_beneficio(idBuscado)
Beneficio.where(id_beneficio: idBuscado).pluck(:id_beneficio).first
end

and then convert the value that i obtain to string with idNumero.to_s

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...