-2
Also ohne immer ich habe 2 Modelle:Ich kann nicht zwei Objekte in der Rails-Konsole verbinden manuell einen NoMethod Fehler
User.rb
class User < ActiveRecord::Base
has_many :pics
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Pic.rb
class Pic < ActiveRecord::Base
belongs_to :user
end
Sie sind jeweils durch eine Eins-zu-Viele-Verknüpfung verknüpft. Ich kann jedoch nicht zwei Objekte in der Rails-Konsole zuordnen, ohne einen NoMethod-Fehler zu empfangen:
2.3.0 :001 > @user = User.first
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id"
ASC LIMIT 1
=> #<User id: 1, email: "[email protected]", created_at: "2016-12-20
10:52:10", updated_at: "2016-12-20 10:52:10">
2.3.0 :002 > @pic = Pic.first
Pic Load (0.1ms) SELECT "pics".* FROM "pics" ORDER BY "pics"."id" ASC
LIMIT 1
=> #<Pic id: 1, title: "Yo! My first post!", description: "Wazzaaappp this
is Wali", created_at: "2016-12-20 10:30:45", updated_at: "2016-12-20
10:30:45", user_id: nil>
2.3.0 :003 > @pic.user = @user
NoMethodError: undefined method `user=' for #<Pic:0x00000002918758>
Did you mean? user_id=
from /usr/local/rvm/gems/ruby-2.3.0/gems/activemodel-
4.2.5/lib/active_model/attribute_methods.rb:433:in `method_missing'
from (irb):3
EDIT: Das ist mein schema.rb
ActiveRecord::Schema.define(version: 20161220102651) do
create_table "pics", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
add_index "pics", ["user_id"], name: "index_pics_on_user_id"
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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name:
"index_users_on_reset_password_token", unique: true
end
hast du '' nachladen Konsole und 'find'! Pic, nachdem Sie Ihre 'Pic.rb' aktualisiert haben – Fallenhero
Ja, es schien nicht laden laden die Konsole funktioniert. –
hast du das Modell auch neu geladen? Denn wenn du es nicht bist, wird es das alte Objekt sein. – Fallenhero