Sis Ccr

Writing custom validation was easy in rails

I had a hotel model and user model. User has many hotels. When creating hotel, I had to validate that it’s email is save as users email associated with it.

class Hotel < ApplicationRecord  validate :check_email?   def check_email?
errors.add :base, "Hotel email must be same as user's email!"
unless self.email == self.user.email
end
end

And that’s it. Please check the spelling of validate. I had some trouble figuring out what was wrong when I mistakely typed validates :check_email?

--

--

Therubyracer in mac Bigsur

Install therubyracer in mac was a pain.

here something worked for me (not in m1 chip though)

brew install v8@3.15
bundle config build.libv8 --with-system-v8
bundle config build.therubyracer --with-v8-dir=$(brew --prefix v8@3.15)
bundle install

--

--

Migrations

Make a field nullable

mix ecto.gen.migration add_nullable_to_field

def change do
alter table(:events) do
modify(:title, :string, null: true, from: :string) # Title column is now nullable
end
end

Add field to existing table

mix ecto.gen.migration add_is_super_admin_to_merchant

Add references to existing table

mix ecto.gen.migration add_district_id_to_room

defmodule Tailwind.Repo.Migrations.AddedDistrictIdToRoom do   use Ecto.Migration
def change do
alter table :rooms do
add :district_id, references( :districts, on_delete: :delete_all)
end
end
end

Add Unique index to a field

I forgot to add unique constraint in email. so generating a migration to add constraint in email.

mix ecto.gen.migration add_uniq_constraint_to_email_on_merchant

Then do mix ecto.migrate

if you got error like “Key (email)=(abc@abc.com) is duplicated.”, that means you already have multiple values in database of same email. Remove those duplicate values and run the migrate command again.

We also need to add unique constraint for email in model .

--

--