嵌套资源 - 关联失败

时间:2014-01-14 03:17:10

标签: ruby-on-rails database nested associations

我有一个用户通过连接表UserEvent has_many事件(和vice_versa)。我应该能够调用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)

1 个答案:

答案 0 :(得分:0)

  

但他们没有填充连接表

那是你的问题

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

我发现你的模型中没有任何accepts_nested_attributes_for方法?这对于正确设置您的加入数据是必要的,以及我认为您未在视图中看到数据的原因

如果您认为合适,我可以编写一些代码吗?