ruby-on-rails - 如何在使用 has_many :through association 时创建关联模型的实例

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

我有两个模型(称为 UserPlan),我使用 has_many :through 关联(称为 Participation)将它们关联起来。计划应该由用户创建,因此我想知道:

如何通过模型 User 的实例访问 create 方法来创建模型 Plan 的实例和连接模型 Participation 的实例

目前我只是想让它在 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

最佳答案

今天早上,我能够使用 User.first.plans.newUser.first.plans.create 创建一个新的 Plan 实例。我唯一的猜测是重启 rails console 成功了。我确定在发布上述问题之前我已经这样做了。

@MilesStanfield:谢谢!由于您的评论,我仔细检查了所有内容。这样做之后,我想再试一次。

关于ruby-on-rails - 如何在使用 has_many :through association 时创建关联模型的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34498283/

相关文章:

ruby-on-rails - Neo4j.rb (8.0.11) + Resque : Input/output error @ io_write - <STDOUT>

ruby-on-rails - rails : token authentication from scratch

postgresql - Postgis – 与 ST_Within 相反

ruby-on-rails - 在 Heroku 的 paper_trail 中自动设置谁负责控制台的更改

css - form_for 上的类标签自动化?这种语法的最佳实践是什么?

postgresql - Prisma 如何查看数据库

postgresql - 计划从 PostgreSQL 导入 Solr4

mongoid - 如何重写 ActiveJob 以防止序列化错误?

ruby-on-rails - Rails 4.2 没有读取 Database.yml 的环境变量

ruby-on-rails - Rails应用程序无法在AWS Elastic Beanstalk上运行,sqlite3错误