Evo*_*o_x 2 ruby-on-rails rails-activerecord
我有 2 个表/模型:路径和问题。每个问题都属于一个路径
我的问题.rb:
class Question < ActiveRecord::Base
belongs_to :path
end
Run Code Online (Sandbox Code Playgroud)
我的路径.rb
class Path < ActiveRecord::Base
has_many :questions
end
Run Code Online (Sandbox Code Playgroud)
一切正常
p = Path.last
Path.questions
Run Code Online (Sandbox Code Playgroud)
返回我需要的一切,但我返回这样的 json 响应:
@path = Path.find_by_id(params[:id])
render :status=>200, :json => {:status => "success", :path => @path, :message => "Showing path"}
Run Code Online (Sandbox Code Playgroud)
该答案当然不包括有关路径的问题。我必须更改什么才能包含属于该路径的所有问题?我知道我可以添加 :path_questions => @path.questions 但是没有新的返回变量就没有办法包含问题吗?我希望我的意思很清楚。
我在 Rails 5 API 应用程序中这样做:
BooksController
def index
@books = Book.limit(params[:limit])
render json: @books, include: ['author'], meta: { total: Book.count }
end
Run Code Online (Sandbox Code Playgroud)
在上述情况下,Book belongs_to Author .
| 归档时间: |
|
| 查看次数: |
1533 次 |
| 最近记录: |