Naji
Naji

Reputation: 732

Rails Model.find() not returning information (returns hash)

I'm using Postgresql and I'm not sure what I'm missing here, but calling my database in my

helper.rb:

def get_info
    info = Scraper.find(1)
    puts info
end

in my controller.rb via a post request, like such:

def create
    helpers.get_info
end

is returning

#<Scraper:0x00007f96f9738ce0>

As opposed to the actual information from my database:

<Scraper id: 1, restaurant: => "subway">

Upvotes: 3

Views: 129

Answers (3)

Ganesh
Ganesh

Reputation: 2004

Modify following code

def get_info
    info = Scraper.find(1)
    puts info
end

as

def get_info
   Scraper.find(1)
end

This will work, no need to use puts info or even if you want to use puts then you can use

puts info.inspect

Upvotes: 3

Tai
Tai

Reputation: 1254

puts prints class and Object Id for complex object. To print details about object, you can use inspect:

puts info.inspect

For more details, you can checkout here: https://vitobotta.com/2011/01/17/prettier-user-friendly-printing-of-ruby-objects/

Upvotes: 2

fabOnReact
fabOnReact

Reputation: 5942

puts info.inspect

I suggest you to read the ruby on rails documentation

Upvotes: 1

Related Questions