This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task remove_duplicate_memberships: :envrionment do | |
# the sub query finds all duplicated memberships | |
# the outer query filters out any duplicates that except the one with the highest ID (per organization) | |
sql = <<-eos | |
select * from memeberships inner join | |
(select max(id) maxid, user_id, organization_id | |
from memberships | |
group by user_id, organization_id | |
having count(1) > 1) as keep | |
on (memberships.user_id = keep.user_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
users_missing_profiles = | |
User.joins("left outer join profiles on users.id = profiles.user_id").where("profiles.id is null") | |
users_missing_profiles.each(&:generate_profile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Which profile is returned? | |
user = User.first | |
# Deterministic | |
# SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 | |
user.profile | |
# Non-deterministic! THERE IS NO ORDER CLAUSE! | |
# SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 LIMIT 1 | |
# add an index to speed up search for duplciates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task remove_invalid_profiles: :envrionment do | |
invalid_profiles = Profile.joins("left outer join users on profile.user_id = users.id") | |
.where("users.id is null") | |
invalid_profiles.destroy_all | |
end | |
class AddForeignKeyConstraintToProfile < ActiveRecord::Migration | |
def change | |
add_foreign_key :profiles, :users, on_delete: :cascade |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task set_proper_timezone: :envrionment do | |
invalid_profiles = Profile.where("timezone is null or timezone not in (?)", ActiveSupport::TimeZone.zones_map.keys) | |
invalid_profiles.each(&:set_default_timezone) | |
end | |
class Profile < ActiveRecord::Base | |
validates :timezone, inclusion: { in: ActiveSupport::TimeZone.zones_map.keys } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task remove_invalid_users: :environment do | |
invalid_users = User.where("email is null or email is ''") | |
# Users without emails probably can't even log in so it's probably best to destroy the records | |
invalid_users.destroy_all | |
end | |
task set_default_locale: :envrionment do | |
invalid_profiles = Profile.where("locale is null or locale = ''") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Organization < ActiveRecord::Base | |
has_many :memberships, dependant: :destroy | |
has_many :users, through: :memberships | |
end | |
class User < ActiveRecord::Base | |
has_many :profiles, dependant: :destroy | |
has_many :memberships, dependant: :destroy | |
has_many :organizations, through: :memberships | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HEADERS = [ | |
"Recrod ID", | |
"Created At", | |
"Updated At", | |
"Error Messages" | |
] | |
def all_models | |
tables = ActiveRecord::Base.connection.tables.sort | |
tables.map { |name| name.classify.safe_constantize }.compact |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeModel < ActiveRecord::Base | |
validates :index, length: { maximum: 25 } | |
end |