Home > Back-end >  How do I display male or female based on certain conditions in Rails?
How do I display male or female based on certain conditions in Rails?

Time:09-04

I am new to rails and currently working on a dating application as a portfolio project. However, on my home page, I want to be able to display/render Male or Female data based on the gender of the current_user. That is if the logged-in user is a male, he will only be able to view people of the opposite gender since it's a dating application.

I am using Devise gem and included a custom field called gender. Please, someone should help me with how to do this.

class UsersController < ApplicationController
  def index
    @user = current_user
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
  end


User Table

create_table "users", force: :cascade do |t|
    t.string "first_name"
    t.string "last_name"
    t.text "short_bio"
    t.string "gender"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    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.string "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string "unconfirmed_email"
    t.integer "like"
    t.string "image_url"
    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 Model

class User < ApplicationRecord
  has_one_attached :image
  has_many_attached :pictures

  has_many :messages
  has_many :likes
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :validatable, :trackable

  # validates :short_bio, presence: true, length: {maximum: 500}
  validates :email, presence: true, uniqueness: { case_sensitive: false }, length: {minimum: 5, maximum: 50}
  

end

CodePudding user response:

You can only load users with the opposite gender than the current user like this:

@users = User.where.not(gender: current_user.gender)

CodePudding user response:

Probably you need in the model two columns. It is just example as idea

You can use gender (it's not binary) instead of sex

You can use other data type of course

t.integer :sex
t.integer :look_for_sex

And for example:

0 -- female, 1 -- male

And then

User.where(sex: current_user.look_for_sex, look_for_sex: current_user.sex)

will return only users that current user want to date

CodePudding user response:

Thank you for all that responded. I didn't have to refactor my code but just added the code below and that fixed my problem:

@users = current_user == 'Male' ? User.where(gender: 'Male') : User.where(gender: 'Female')

In this case, if the current user is a 'Male', he will only have access to 'Female' and vice versa.

  • Related