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

Posted

技术标签:

【中文标题】Rails 关联不起作用,user_id 未显示在控制台中【英文标题】:Rails Associations Not working, user_id not showing in console 【发布时间】:2016-08-04 12:46:12 【问题描述】:

在控制台中我收到此错误:

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

【问题讨论】:

【参考方案1】:

确保您的数据库是最新的。

$ 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

【讨论】:

试过了。仍然收到错误'NoMethodError:未定义的方法user' for #&lt;Peegin:0x007fb61a530de0&gt; from /Users/Amani/.rvm/gems/ruby-2.3.0/gems/activemodel-4.2.6/lib/active_model/attribute_methods.rb:433:in method_missing'' 是的,我得到了这个 bin/rake db:migrate 在进程 26378 /Users/Amani/.rvm/gems/ruby-2.3.0/gems/spring-1.7 中通过 Spring 预加载器运行.2/lib/spring/application.rb:175:警告:PATH 中不安全的世界可写目录 /usr/local,模式 040777【参考方案2】:

请在您的 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

【讨论】:

谢谢它现在似乎可以工作了。我关闭了 Rails 服务器并重新启动它,一切都开始工作了。

以上是关于Rails 关联不起作用,user_id 未显示在控制台中的主要内容,如果未能解决你的问题,请参考以下文章

Rails 与自身的关联不起作用

带有has_many的模型中的属性总和:通过关联在Rails 5中不起作用

使用dependent: :destroy 在rails 上不起作用

Rails 4:条件显示在视图内不起作用

has_one 与用户和客户的关联以及 Rails 视图表单

Has_many:通过 Rails 4 实例变量不起作用