简体   繁体   English

Rails关联不起作用,user_id未显示在控制台中

[英]Rails Associations Not working, user_id not showing in console

In console I get this error: 在控制台中,我得到此错误:

peegin.user
NameError: undefined local variable or method `peegin' for main:Object
Did you mean?  @peegin

Also when i try to access Peegin in console it doesn't display user_id : 另外,当我尝试在控制台中访问Peegin时,它不显示user_id

Peegin
 => Peegin(id: integer, title: string, meaning: string, example: string, created_at: datetime, updated_at: datetime, permalink: string) 
2.3.0 :039 >

User Class 用户类别

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

has_many :peegins
end

Peegin Class Peegin类

class Peegin < ActiveRecord::Base
belongs_to :user
before_create :generate_permalink
def to_param
    permalink
end



private

def generate_permalink
    pattern=self.title.parameterize
    duplicates = Peegin.where(permalink: pattern)

    if duplicates.present?
        self.permalink = "#{pattern}-#{duplicates.count+1}"
    else
        self.permalink = self.title.parameterize
    end

 end

end

Schema 架构图

ActiveRecord::Schema.define(version: 20160804115242) do
create_table "peegins", force: :cascade do |t|
t.string   "title"
t.string   "meaning"
t.string   "example"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string   "permalink"
t.integer  "user_id"
end

add_index "peegins", ["user_id"], name: "index_peegins_on_user_id", unique:    true

 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
 t.string   "name"
 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

Make sure your database is up to date. 确保您的数据库是最新的。

$ bin/rake db:migrate

Then.. you have to create a Peegin object and then you can access the user: 然后..您必须创建一个Peegin对象,然后可以访问用户:

peegin = Peegin.create(title: "something")
peegin.user # => nil
peegin.user = User.first # Or take any user you want.
peegin.save # => user_id is updated in the database

In your rails console please try this: 在您的Rails控制台中,请尝试以下操作:

@peegin = Peegin.find(1) //IF one doesn't work make sure if database has a record with this id and try with id that exists.

then use 然后使用

@peegin.user 

Instead of 代替

peegin.user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM