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

trying to create a one to one relationship

john seymour
1#
john seymour Published in 2018-02-04 09:25:43Z

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

Yunwei.W
2#
Yunwei.W Reply to 2018-02-04 09:43:01Z

Usually you will want to call

user.profile.country

But if you want to use user.country you will have to delegate those methods from Profile to User like this:

   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
Anees Muhammed
3#
Anees Muhammed Reply to 2018-02-04 09:39:34Z

All your association setup is correct but I think you've slightly misunderstood the way associations in rails works. Since you have the associations user has_one profile and profile belongs_to user You can call @user.profile which will return you the profile object associated with the user. To access the country attribute in profile, you have to actually call, @user.profile.country. So instead of calling

user.country 

You have to use

user.profile.country

Hope this helps.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO