Sebastian Zeki
Sebastian Zeki

Reputation: 6874

Show all records in a database table rather than individual records

I'm pretty new to rails. I'm learning how to pass database queries to json to render them in a view.

I would like to perform a simple one table query (table is called user name:string id:integer) which is performed in the model and passed to the controller and then to index.json.jbuilder to index.html.erb.

The problem is that when I call

blabla/index.json I get an empty array but when I call blabla/1.json I get the data of user 1, but I want to get all the users.

I am aware there are easier ways to do this but this is how I would like to do this. How can I avoid the need for passing the user_id to get the records?

user.rb

class User < ActiveRecord::Base

def self.feed
"SELECT * FROM User"
end
 end

users_controller.rb

   def index
     @users = User.feed
   end

index.json.jbuilder

 json.extract! @user, :id, :name, :created_at, :updated_at

index.html.erb

 <p id="notice"><%= notice %></p>
 <h1>Listing Users</h1>
 <table>
   <thead>
     <tr>
       <th>Name</th>
       <th colspan="3"></th>
     </tr>
   </thead>
  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td><%= user.name %></td>
        <td><%= link_to 'Show', user %></td>
        <td><%= link_to 'Edit', edit_user_path(user) %></td>
        <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
 </table>
 <br>
 <%= link_to 'New User', new_user_path %>

Upvotes: 0

Views: 793

Answers (2)

Jonathan Reyes
Jonathan Reyes

Reputation: 1419

You can use user123's answer and probably should because the ORM can become super powerful when tied with ACL gems, caching, etc. But you can also write more complex queries if you need to. Here is an example of a more complex select users by distance from latitude and longitude using a scope (you can also use in a function):

scope :near, -> (lat, lng, distance) {
  origin_lat = lat / 180 * Math::PI
  origin_lng = lng / 180 * Math::PI

  select("users.*")
  .select("(ACOS(COS(%{lat})*COS(%{lng})*COS(RADIANS(users.latitude))*COS(RADIANS(users.longitude))+
      COS(%{lat})*SIN(%{lng})*COS(RADIANS(users.latitude))*SIN(RADIANS(users.longitude))+
      SIN(%{lat})*SIN(RADIANS(users.latitude)))*3963) AS distance" % [ lat: origin_lat, lng: origin_lng, distance: distance ])
  .where(["(ACOS(COS(:lat)*COS(:lng)*COS(RADIANS(users.latitude))*COS(RADIANS(users.longitude))+
      COS(:lat)*SIN(:lng)*COS(RADIANS(users.latitude))*SIN(RADIANS(users.longitude))+
      SIN(:lat)*SIN(RADIANS(users.latitude)))*3963) <= :distance", lat: origin_lat, lng: origin_lng, distance: distance])
  .order('distance DESC')
}

Where you would then call it like User.near(lat, lng, distance) and it will return all the instances that are within a certain distance.

When it comes down to it, its just a matter of knowing where to place the strings within Rails' query builder.

Upvotes: 0

Vrushali Pawar
Vrushali Pawar

Reputation: 3803

def index
  @users = User.get_all
  respond_to do |format|
    format.json {render json: @users}
  end
end

in user.rb

def self.get_all
  User.all
end

Upvotes: 2

Related Questions