Search code examples
ruby-on-railsarrayssqlitesqlite3-ruby

How to extract data from sqlite3 array in ruby on rails?


How can I get data from an array of the form:

[#<User id: 1, name: "John", surname: "Smith", dob: "2016-07-26", location: "Liverpool", created_at: "2016-07-26 08:50:01", updated_at: "2016-07-26 08:50:01">]

generated from sqlite3 database using this code:

<%= User.select(User.all.select { |u| u.id == 1 }) %>

Also, is there any better way to extract selected fields than everything? Whatever I tried returns some long random numbers like references.

And finally, how can I make the:

u.id == 1

to become any id given to that user in real time, like the following:

u.id == x (where x is any number)

Cheers!


Solution

  • I don't know if I'm missing something but I think what you want is either find:

    @user = User.find(x) # x = given id
    

    or where (returns a set of users, not just one)

    @user = User.where(id: x)
    

    And then in your view you can use the user like this:

    <%= @user.name %>