Answer the question
In order to leave comments, you need to log in
How to make a selection in ROR?
Hello.
I started studying rails and almost immediately ran into a banal problem.
The situation is this: there are buyers and each buyer has several orders.
The models look like this:
class User < ActiveRecord::Base
has_many :order
end
class Order < ActiveRecord::Base
belongs_to :user
end
class UserController < ApplicationController
def index
@user = User.all
render json: @user
end
end
Answer the question
In order to leave comments, you need to log in
First, has_many :orders
after all (in the plural)
In a simple way, you can do something like this:
class UsersController < ActionController::Base
def index
@users = Hash[User.all.map{ |u| [u.name, u.orders.to_a }].to_json
end
end
@users = User.all
, and then process orders in view. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question