Ruby on rails 我能';t手动关联Rails控制台中的2个对象,而不会出现NoMethod错误
所以我有两个模型: User.rbRuby on rails 我能';t手动关联Rails控制台中的2个对象,而不会出现NoMethod错误,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,所以我有两个模型: 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, :reco
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
您必须重新启动/重新加载控制台才能使更改生效您是否
重新加载代码>你的控制台和更新图片后的查找图片。rb
是的,重新加载控制台似乎不起作用。你也重新加载了模型吗?因为如果你不这么做,那将是老问题不,我怎么能?对不起,我是Rails新手。我试图在控制台中调用User.reload,但收到一个nomethod错误。只需@pic=pic。首先即可
class Pic < ActiveRecord::Base
belongs_to :user
end
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: "nono@gmail.com", 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
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