2016-08-04 65 views
0

在控制台中我得到这个错误:Rails的协会不工作,在控制台USER_ID不显示

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

还当我尝试访问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 > 

用户类

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 < 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 

架构

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 

回答

0

确保数据库是最新的。

$ bin/rake db:migrate 

然后..你必须创建一个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 
+0

试过了。仍然从/Users/Amani/.rvm/gems/ruby-2.3.0/gems/activemodel-4.2.6/lib/active_model/attribute_methods收到错误'NoMethodError:undefined method'user'for# \t .rb:433:in'method_missing'' – AMANi

+0

您是否运行过上述命令? – siegy22

+0

是的,我得到了_bin/rake db:migrate 在进程26378 /Users/Amani/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application中通过Spring预加载器运行.rb:175:警告:PATH中不安全的可写dir/usr/local,模式040777_ – AMANi

0

在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. 

然后使用

@peegin.user 

而不是

peegin.user 
+0

感谢它似乎现在正在工作。我关闭rails服务器,并开始回来,一切开始工作。 – AMANi