Ruby on Rails 4.0的新功能,我在使用JSON从连接表中添加数据时遇到了一些麻烦.我在前端使用AngularJS,我似乎无法在这里找到一个很好的例子来帮助我.
简而言之,我有一个带有用户ID的邀请.我有一个用户ID的用户.知道邀请ID后,我想获得与该邀请对应的用户名.
Invite 1: UserID: 10 Invite 2: UserID: 11 Invite 3: UserID: 12 UserID: 10 Name: Owen UserID: 11 Name Greg
控制器 - 邀请
# GET /invites/1 # GET /invites/1.json def show @invite = Invite.includes(:user).find(params[:id]).to_json(include: :user) end
型号 - 邀请
class Invite < ActiveRecord::Base belongs_to :user has_one :meal has_one :event has_one :registry end
型号 - 用户
class User < ActiveRecord::Base has_one :invite end
但是每当我检查/ invites/1时我都会得到这个.
{"id":1,"created_at":"2013-12-06T20:14:39.001Z","updated_at":"2013-12-06T20:58:15.597Z","event_id":7,"meal_id":8,"registry_id":0,"rsvp":false,"user_id":9}
这里的任何帮助非常感谢!