如何在使用has_many:through关联时创建关联模型的实例

时间:2015-12-28 17:51:27

标签: ruby-on-rails postgresql ruby-on-rails-4.2 ruby-2.2

我有两个模型(称为用户计划),我使用has_many:through关联(称为参与)关联。计划应该由用户创建,因此我想知道:

如何通过模型用户访问create方法来创建模型计划的实例和连接模型实例参与

目前我只是想让它在rails控制台中运行,但我当然希望在我的应用程序中使用该功能。我不使用HABTM关联,因为下一步是向关系添加属性。

我可以通过输入

来访问现有的Plan计划实例
 > User.first.plans.all

进入Rails控制台。但当我尝试

> User.first.plan.new

> User.first.plans.new

> User.first.plan.create

> User.first.plans.create

我收到错误消息

NoMethodError: undefined method `plan' for #<User:

NoMethodError: undefined method `plans' for #<User:

额外信息: 相应的型号:

class User < ActiveRecord::Base

  has_many :participations            #, :foreign_key => ":user_id"
  has_many :plans, :through => :participations
  accepts_nested_attributes_for :participations, :plans

end

class Plan < ActiveRecord::Base

  has_many :participations            #, :foreign_key => ":plan_id"
  has_many :users, :through => :participations
  accepts_nested_attributes_for :participations, :users

end

class Participation < ActiveRecord::Base
  belongs_to :user            #, inverse_of: :participations
  belongs_to :plan            #, inverse_of: :participations

  validates :user_id, presence: true
  validates :plan_id, presence: true

end
架构是:

  create_table "participations", force: :cascade do |t|
    t.integer  "user_id",    null: false
    t.integer  "plan_id",    null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "participations", ["plan_id"], name: "index_participations_on_plan_id", using: :btree
  add_index "participations", ["user_id"], name: "index_participations_on_user_id", using: :btree

  create_table "plans", force: :cascade do |t|
    t.text     "title"
    t.text     "description"
    t.text     "plan_type"
    t.datetime "start_date"
    t.datetime "end_date"
    t.integer  "parent_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  add_index "plans", ["parent_id"], name: "index_plans_on_parent_id", using: :btree

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "username",                            null: false
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
  add_index "users", ["username"], name: "index_users_on_username", unique: true, using: :btree

end

1 个答案:

答案 0 :(得分:0)

今天早上我可以使用User.first.plans.newUser.first.plans.create来创建一个新的Plan实例。我唯一的猜测是重启rails console就行了。在发布上述问题之前,我是肯定的。

@ MilesStanfield:Thanx!由于你的评论,我仔细检查了一切。在这之后,我想再试一次。