2

I have two models (Called User and Plan) that I have associated by using a has_many :through association (called Participation). A plan should be created by a user and therefore I want to know:

How to create an instance of the model Plan and an an instance of the join model Participation by accessing the create method through an instance of the model User

At the moment i am just trying to get it to work in rails console but of course I want to use the functionality in my application. I don't use a HABTM association because the next step is to add an attribute to the relation.

I am able to access existing instances of Plan by entering

 > User.first.plans.all

in to Rails console. but when I try either

> User.first.plan.new

or

> User.first.plans.new

or

> User.first.plan.create

or

> User.first.plans.create

I get an error that states

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

or

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

Extra info: corresponding models:

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

schema is:

  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
domtiedom
  • 61
  • 5
  • Looks right to me and it's strange that ```User.first.plans.all``` works but ```User.first.plans.create(some_attr: "foo")``` doesn't. I think it's worth asking **are you sure you're using rails 4.2?** – MilesStanfield Dec 29 '15 at 00:25
  • I can't get my head around it either. But `rails -v` and `bundle show rails` return `Rails 4.2.2` and `~/.rvm/gems/ruby-2.2.1/gems/rails-4.2.2` in terminal. – domtiedom Dec 29 '15 at 08:16

1 Answers1

1

This morning I was able to use User.first.plans.new and User.first.plans.create to create a new instance of Plan. My only guess is that restarting rails console did the trick. I am positive I did that before I posted the above question.

@ MilesStanfield: Thanx! Because of your comment I double checked everything. After doing this I thought to give another try.

domtiedom
  • 61
  • 5