Adds new personal detail fields to Users table#473
Conversation
| # reset_password_sent_at :datetime | ||
| # reset_password_token :string | ||
| # sign_in_count :integer default(0), not null | ||
| # social_accounts :jsonb |
There was a problem hiding this comment.
I would suggest to have a separate model for social_accounts with a has_many :social_accounts association on User model. Fits well with the relational model.
There was a problem hiding this comment.
We decided to go ahead with a JSON field type @apoorv-mishra so that it will be easier for us to add more social links without migrating DB columns. Also, we only need to store links as names and URLs as strings which we could nicely store in a JSON. It will also avoid a join query.
app/models/user.rb
Outdated
| store_accessor :social_accounts, :github_url, :linkedin_url | ||
|
|
||
| # Validations | ||
| before_validation :set_default_social_accounts, on: :create |
There was a problem hiding this comment.
Can you comment on why did you choose the before_validation callback? I think we can have it after_create
Also, we are not checking if the URLs already exist in the object. Wouldn't this overwrite the URLs we actually want to save?
There was a problem hiding this comment.
Hey, @akhilgkrishnan suggested creating empty strings by default when the User object is created. @akhilgkrishnan please comment.
There was a problem hiding this comment.
I think, In this case, I don't think we need an after_create. The problem with after_create it actually write the DB multiple times.
One for creating the user entry, another write for updating the social_account. before_validation or before_create is suitable for here, because they are just initializing the value and not creating an entry for the DB.
There was a problem hiding this comment.
Let's go ahead with before_create 👍
| @@ -0,0 +1,17 @@ | |||
| # frozen_string_literal: true | |||
|
|
|||
| require_relative("./verifiers/user_default_social_account_verifier.rb") | |||
There was a problem hiding this comment.
Do we need this to be in a separate directory? We could just write the method in the migration itself. Could you post any blog or tutorial you followed for this over here for my reference?
There was a problem hiding this comment.
Hi, its created so that we can add more verifiers for future data migrations.
app/models/user.rb
Outdated
| def set_default_social_accounts | ||
| self.social_accounts = { | ||
| "github_url": "", | ||
| "linkedin_url": "" | ||
| } | ||
| end | ||
|
|
There was a problem hiding this comment.
@mohinid Creating empty strings is fine. before_validation :set_default_social_accounts, on: :create can be replaced with before_create as it would be equivalent and I think it should be after_create cc @akhilgkrishnan
There was a problem hiding this comment.
Yes, before_create also works
There was a problem hiding this comment.
@akhilgkrishnan @rohitjoshixyz what is the final conclusion? before_create or before_validation?
There was a problem hiding this comment.
@akhilgkrishnan @rohitjoshixyz I am using "after_initialize" with a check of new_record. It creates social_accounts with empty URLs because 'before_create' is generating an empty social_accounts object.
Tested this by updating the github_url of the first record, as well as running RSpecs. Here is the SS.
| # updated_at :datetime not null | ||
| # current_workspace_id :bigint | ||
| # invited_by_id :bigint | ||
| # personal_email_id :string |
There was a problem hiding this comment.
@mohinid Still got confused with this field. What's the exact difference of the actual email and this one.
There was a problem hiding this comment.
This is the personal Email Id as of the user just for reference. Like an alternative email. Screenshot attached in the notion task.
Current Code Coverage Percent of this PR:89.15 %Files having coverage below 100%
|
rohitjoshixyz
left a comment
There was a problem hiding this comment.
LGTM, I like the before_initialize callback 👍
* Adds new personal detail fields to Users table * update callback to after_initialize Co-authored-by: Akhil G Krishnan <[email protected]>
Notion card
https://www.notion.so/saeloun/Add-tables-for-personal-employment-Allocated-devices-compensation-71ef49d0c38e4242bd2998120141372d
Summary
Preview
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Tested on Local Rails console.
Checklist: