【问题标题】:trying to create a one to one relationship试图建立一对一的关系
【发布时间】:2018-02-04 09:25:43
【问题描述】:

我正在尝试建立一种我认为我理解的一对一关系......

我有我的用户表和配置文件表,它们应该以一对一的关系链接,但是当我尝试调用它时,它会返回一个错误。

这是我的模型:

    class Profile < ApplicationRecord
      mount_uploader :AvatarUploader
      belongs_to :user
    end



    class User < ApplicationRecord
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable, :omniauthable,
             :recoverable, :rememberable, :trackable, :validatable,
             :confirmable
      acts_as_voter
      enum role: [:user, :admin]
      has_many :entries, dependent: :destroy
      has_many :reports, dependent: :destroy
      has_many :messages
      has_one :profile
    end

我的架构中的相关表:

      create_table "profiles", force: :cascade do |t|
        t.text "bio"
        t.string "avatar"
        t.string "country"
        t.bigint "user_id"
        t.datetime "created_at", null: false
        t.datetime "updated_at", null: false
        t.index ["user_id"], name: "index_profiles_on_user_id"
      end




        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.inet "current_sign_in_ip"
        t.inet "last_sign_in_ip"
        t.string "confirmation_token"
        t.datetime "confirmed_at"
        t.datetime "confirmation_sent_at"
        t.string "unconfirmed_email"
        t.datetime "created_at", null: false
        t.datetime "updated_at", null: false
        t.integer "role", default: 0
        t.integer "score", default: 0
        t.string "username"
        t.index ["email"], name: "index_users_on_email", unique: true
        t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
      end

所以我正在尝试调用返回无方法错误的 user.country。我已经阅读了几个不同网站上的关系,但似乎没有注意到我做错了什么

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-5


    【解决方案1】:

    通常你会想打电话

    user.profile.country
    

    但是,如果您想使用 user.country,您必须将这些方法从 Profile 委托给 User,如下所示:

       class User < ApplicationRecord
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable, :omniauthable,
             :recoverable, :rememberable, :trackable, :validatable,
             :confirmable
      acts_as_voter
      enum role: [:user, :admin]
      has_many :entries, dependent: :destroy
      has_many :reports, dependent: :destroy
      has_many :messages
      has_one :profile
    
      #delegate method call to profile, check the delegate document for more options.
      delegate :country, to: :profile
    
    
    end
    

    【讨论】:

      【解决方案2】:

      您的所有关联设置都是正确的,但我认为您稍微误解了 rails 中关联的工作方式。因为你有关联 user has_one profileprofile belongs_to user 您可以调用@user.profile,它将返回与用户关联的配置文件对象。要访问配置文件中的country 属性,您必须实际调用@user.profile.country。所以不要打电话

      user.country 
      

      你必须使用

      user.profile.country
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-22
        • 1970-01-01
        • 2023-04-11
        • 2011-06-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多