Home trying to create a one to one relationship
Reply: 0

trying to create a one to one relationship

user5867
1#
user5867 Published in September 20, 2018, 1:16 pm

I am trying to create a one to one relationship to which i thought i understood...

I have my user table and a profile table which should be linked with a one to one relationship but when i try to call on it, it returns an error.

Here is are my models:

    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

The relevant tables in my schema:

      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

So I'm trying to call user.country which is returning a no method error. Ive read up on the relationship on a few different sites but cant seem to notice what i'm doing wrong

Thanks

share|improve this question

2 Answers 2

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.641698 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO