Ruby on rails 嵌套资源-关联失败

Ruby on rails 嵌套资源-关联失败,ruby-on-rails,database,nested,associations,Ruby On Rails,Database,Nested,Associations,我有一个用户通过联接表UserEvent拥有许多事件(反之亦然)。我应该能够调用current_user.events来获取用户创建的事件列表。正在创建事件,但它们没有填充联接表,因此我可以调用user.Events。为什么? class User < ActiveRecord::Base has_many :user_events, inverse_of: :user has_many :events, through: :user_events has_ma

我有一个用户通过联接表UserEvent拥有许多事件(反之亦然)。我应该能够调用current_user.events来获取用户创建的事件列表。正在创建事件,但它们没有填充联接表,因此我可以调用user.Events。为什么?

class User < ActiveRecord::Base
  has_many :user_events,
    inverse_of: :user
  has_many :events,
    through: :user_events
  has_many :user_activities,
    inverse_of: :user
  has_many :activities,
    through: :user_activities

  validates_presence_of :first_name
  validates_presence_of :last_name
  validates_email_format_of :email
  validates_presence_of :role

  def is_admin?
    role == 'admin'
  end
end


class UserEvent < ActiveRecord::Base
  belongs_to :event
  belongs_to :user

  validates_presence_of :user
  validates_presence_of :event
  validates_numericality_of :user_id
  validates_numericality_of :event_id
end

class Event < ActiveRecord::Base
  validates_presence_of :start_time
  validates_time :start_time, before: :end_time
  validates_presence_of :end_time
  validates_time :end_time
  validates_presence_of :description
  validates_presence_of :city
  validates_presence_of :state
  validates_presence_of :activity_id

  belongs_to :activity,
    inverse_of: :events
  has_many :user_events,
    inverse_of: :event
  has_many :users,
    through: :user_events
  has_many :comments,
    inverse_of: :event
end



class UsersController < ApplicationController
  before_filter :authenticate_user!

  def index
    @users = User.all
    @activities = Activity.all
    @events = Event.all
  end

  def show
    @user = current_user
    @events = Event.all
    @activities = Activity.all
  end

  def update
    if current_user.update(user_params)
      current_user.save
      redirect_to user_path, notice: 'User Information Successfully Updated!'
    else
      redirect_to user_activities_path
    end
  end

  private
  def user_params
    params.require(:user).permit(:first_name, :last_name, :email, activity_ids: [], event_ids: [])
  end
end


class EventsController < ApplicationController
  before_filter :authenticate_user!

  def index
    @events = Event.all

  end

  def new
    @event = Event.new
    @activities = Activity.all

  end

  def create
    @event = current_user.events.build(event_params)#builds the event for the current user
    if @event.save
      redirect_to user_event_path(current_user, @event), notice: 'Event Created Successfully' #the route requires two inputs to get to the right place. check rake routes to see.
    else
      render :new
    end
  end

  def show
    @event = Event.find(params[:id])
    @comment = Comment.new
  end

  def edit
    @event = Event.find(params[:id])
  end

  def update
    @event = Event.find(params[:id]) # has to match Edit with params[:id] otherwise undefined method `update' for nil:NilClass
    if @event.update(event_params)
      redirect_to user_event_path(current_user, @event), notice: 'Successfully Updated!'
    else
      render :edit
    end
  end

  private

  def event_params
    params.require(:event).permit(:start_time, :end_time, :city, :state, :description, :location_name, :street_address, :num_attendees_requested, :activity_id)
  end
end






Started POST "/users/1/events" for 127.0.0.1 at 2014-01-14 07:27:43 -0500
Processing by EventsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"d0MIFBOcK4vjs0cQk6mCcbjrJjB23VTnYIeEwOwiK8k=", "event"=>{"activity_id"=>"3", "start_time(1i)"=>"2014", "start_time(2i)"=>"1", "start_time(3i)"=>"14", "start_time(4i)"=>"14", "start_time(5i)"=>"27", "end_time(1i)"=>"2014", "end_time(2i)"=>"1", "end_time(3i)"=>"14", "end_time(4i)"=>"17", "end_time(5i)"=>"27", "num_attendees_requested"=>"4", "location_name"=>"Streets", "street_address"=>"123 All", "city"=>"Boston", "state"=>"MA", "description"=>"Biking all over the place"}, "commit"=>"Create Event", "user_id"=>"1"}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
   (0.2ms)  BEGIN
  SQL (60.9ms)  INSERT INTO "events" ("activity_id", "city", "created_at", "description", "end_time", "location_name", "num_attendees_requested", "start_time", "state", "street_address", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id"  [["activity_id", 3], ["city", "Boston"], ["created_at", Tue, 14 Jan 2014 12:27:43 UTC +00:00], ["description", "Biking all over the place"], ["end_time", Tue, 14 Jan 2014 17:27:00 UTC +00:00], ["location_name", "Streets"], ["num_attendees_requested", 4], ["start_time", Tue, 14 Jan 2014 14:27:00 UTC +00:00], ["state", "MA"], ["street_address", "123 All"], ["updated_at", Tue, 14 Jan 2014 12:27:43 UTC +00:00]]
   (21.2ms)  COMMIT
Redirected to http://localhost:3000/users/1/events/10
Completed 302 Found in 91ms (ActiveRecord: 82.8ms)


Started GET "/users/1/events/10" for 127.0.0.1 at 2014-01-14 07:27:43 -0500
Processing by EventsController#show as HTML
  Parameters: {"user_id"=>"1", "id"=>"10"}
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
  Event Load (0.4ms)  SELECT "events".* FROM "events" WHERE "events"."id" = $1 LIMIT 1  [["id", "10"]]
  Activity Load (1.0ms)  SELECT "activities".* FROM "activities" WHERE "activities"."id" = $1 ORDER BY "activities"."id" ASC LIMIT 1  [["id", 3]]
  Comment Load (0.7ms)  SELECT "comments".* FROM "comments" WHERE "comments"."event_id" = $1  [["event_id", 10]]
  Rendered events/show.html.erb within layouts/application (58.9ms)
Completed 200 OK in 129ms (Views: 103.5ms | ActiveRecord: 4.1ms)
class用户“✓", "真实性令牌“=>”D0MIFBOCK4VJS0CQK6MCCJB23VTNYIEEWIK8K=”,“事件”=>{“活动id”=>“3”,“开始时间(1i)”=>“2014”,“开始时间(2i)”=>“1”,“开始时间(3i)”=>“14”,“开始时间(4i)”=>“14”,“开始时间(5i)”=>“27”,“结束时间(1i)”=>“2014”,“结束时间(2i)”=>“1”,“结束时间(14I)”、“结束时间(17)”,“num_Attenders_requested”=>“4”、“location_name”=>“Streets”、“street_address”=>“123 All”、“city”=>“Boston”、“state”=>“MA”、“description”=>“各地骑车”}、“commit”=>“Create Event”、“user_id”=>“1”}
用户加载(0.5ms)从“用户”中选择“用户”。*其中“用户”。“id”=1“用户”的订单。“id”ASC限制1
(0.2ms)开始
SQL(60.9ms)插入“事件”(“活动id”、“城市”、“创建时间”、“描述”、“结束时间”、“地点名称”、“请求的出席人数”、“开始时间”、“州”、“街道地址”、“更新时间”)值($1、$2、$3、$4、$5、$6、$7、$8、$9、$10、$11)返回“id”[[“活动id”,3],“城市”、“波士顿”],[“创建时间”)“,2014年1月14日,星期二12:27:43 UTC+00:00],“描述”,“骑自行车到处走”],[“结束时间”,2014年1月14日,星期二17:27:00 UTC+00:00],“地点名称”,“街道”],[“要求出席人数”,4],“开始时间”,2014年1月14日,星期二14:27:00 UTC+00:00],“州”,“马”,[“街道地址”,“123全部”],[“更新时间”,2014年1月14日星期二12:27:43 UTC+00:00]]
(21.2ms)提交
重定向到http://localhost:3000/users/1/events/10
91毫秒内完成302次(ActiveRecord:82.8毫秒)
2014-01-14 07:27:43-0500开始获取127.0.0.1版本的“/users/1/events/10”
由EventsController处理#显示为HTML
参数:{“用户id”=>“1”,“id”=>“10”}
用户加载(0.4ms)从“用户”中选择“用户”。*其中“用户”。“id”=1“用户”的订单。“id”ASC限制1
事件加载(0.4ms)从“事件”中选择“事件”。*其中“事件”。“id”=$1限制1[[“id”,“10”]]
活动负载(1.0ms)选择“活动”。*从“活动”中选择“活动”。“id”=$1订单由“活动”。“id”ASC限制1[[“id”,3]]
评论加载(0.7ms)选择“评论”。*从“评论”中选择“评论”。“事件id”=$1[[“事件id”,10]]
布局/应用程序中的呈现事件/show.html.erb(58.9ms)
在129ms内完成200 OK(视图:103.5ms |活动记录:4.1ms)
但是它们没有填充联接表

那是你的问题

使用关系时,父对象检索子对象的唯一方法是通过联接表

我看你的模型里没有任何方法?这是正确设置联接数据所必需的,并且