Skip to content

Models, Associations & Database schema based on ERD including tests - #5

Open
wra-wtag wants to merge 45 commits into
Initiate-Railsfrom
core-models-and-associations-based-on-erd-with-tests
Open

wra-wtag wants to merge 45 commits into
Initiate-Railsfrom
core-models-and-associations-based-on-erd-with-tests

Conversation

@wra-wtag

@wra-wtag wra-wtag commented Sep 23, 2025 •

Copy link
Copy Markdown
Owner

Models, Associations & Database Schema Setup

Trello Ticket: Models, Associations & Database Schema Setup

Description

Created all core database models and their associations based on the ERD. Added validations and unit tests for all models to ensure correctness.

Changes

  • Models created: User, Company, Job, Application, RecruiterMembership, Bookmark, Notification, Skill, JobRecommendation
  • Added associations between models
  • Added validations for required fields
  • Wrote unit tests for all models
  • Verified models work correctly in Rails console

@wra-wtag wra-wtag changed the title User model creation Model Creation and Testing Sep 24, 2025
@wra-wtag wra-wtag changed the title Model Creation and Testing Models, Associations & Database schema based on ERD including tests Sep 30, 2025
@wra-wtag
wra-wtag changed the base branch from Gems to Initiate-Rails October 2, 2025 05:18
Comment thread app/models/application.rb
@@ -0,0 +1,32 @@
class Application < ApplicationRecord
belongs_to :job, counter_cache: :applications_count

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we using counter_cache here? Can you please explain?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @hoh-wtag bhai, The counter_cache is a Rails performance optimization feature. Without counter_cache: if we wanted to list out all the applications or total applications count, we had to perform another query on the database. By using counter_cache, we are actually reducing that extra query, this is related to the N+1 Query problem i studied in Rails Docs.

Thanks bhai.

Comment thread db/schema.rb
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.0].define(version: 2025_09_24_090332) do

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have all your migrations in this PR, but how does the changes in schema file exist in the base branch?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hoh-wtag bhai, sorry for that. It happened because i pulled the initiate-rails branch here and then mistakenly accepted incoming changes of the schema file in the current branch. that's why it happened.


# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
config.action_mailer.raise_delivery_errors = true

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain what's the purpose of changing thos to true?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @hoh-wtag bhai, I actually set it to true because i wanted the application to raise an error whenever it is failed to send an email. If set to false, the application will silently run without raising any error, however just sending the error in the log.

Comment thread app/models/application.rb
belongs_to :job, counter_cache: :applications_count
belongs_to :user

enum :status, { applied: 0, viewed: 1, shortlisted: 2, rejected: 3, hired: 4, withdrawn: 5 }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validates :status, inclusion: { in: statuses.keys }

Do we need additional validation for enum?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hoh-wtag bhai, added the validation. Thank you.

Comment thread app/models/application.rb

validates :job_id, uniqueness: { scope: :user_id, message: "You have already applied for this job" }

has_one_attached :resume

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add validations on the file types of the reswume, so that users cannot upload any kinds of files?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hoh-wtag added the validation for resume, Thank you.

Comment thread app/models/job_recommendation.rb Outdated
end

def recommended_jobs
return [] unless payload["job_ids"].present?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use jsonb in the DB and cast it in the model using Rails’ typed attributes

store_accessor :payload, :job_ids

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hoh-wtag bhai, fixed it. Thank you

Comment thread app/models/recruiter_membership.rb Outdated
Comment on lines +10 to +15
scope :managers, -> { where(role: :manager) }
scope :standard, -> { where(role: :standard) }
scope :primary, -> { where(is_primary: true) }
scope :pending, -> { where(status: :pending) }
scope :approved, -> { where(status: :approved) }
scope :rejected, -> { where(status: :rejected) }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hoh-wtag bhai, fixed. Thank you

Comment thread app/models/skill.rb
Comment on lines +4 to +5
has_many :user_skills, dependent: :destroy
has_many :users, through: :user_skills

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use has_many :users, through: :user_skills do we need has_many :user_skills, dependent: :destroy seperately?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hoh-wtag bhai, without dependent: :destroy on user_skills, rails won't auto-delete UserSkill rows. We might end up with a foreign key error blocking deletion if we use DB FKs.

Comment thread app/models/user_skill.rb Outdated
@@ -0,0 +1,9 @@
class UserSkill < ApplicationRecord
validates :user_id, uniqueness: { scope: :skill_id }
validates :experience_years, numericality: { greater_than_or_equal_to: 0 }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
validates :experience_years, numericality: { greater_than_or_equal_to: 0 }
validates :years_of_experience, numericality: { greater_than_or_equal_to: 0 }

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hoh-wtag bhai, fixed. Thank you.

Comment thread Gemfile Outdated

gem "devise"
gem "email_validator"
gem "grape"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need grape in this PR?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hoh-wtag bhai, deleted grape gem. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants