Changing type of ActiveRecord Class in Rails with Single Table Inheritance

Steve’s answer works but since the instance is of class BaseUser when save is called, validations and callbacks defined in User will not run. You’ll probably want to convert the instance using the becomes method: user = BaseUser.where(email: “[email protected]”).first_or_initialize user = user.becomes(User) # convert to instance from BaseUser to User user.type = “User” user.save!

When is touch for a belongs_to in Rails triggered?

From: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html :touch If true, the associated object will be touched (the updated_at/on attributes set to now) when this record is either saved or destroyed. If you specify a symbol, that attribute will be updated with the current time in addition to the updated_at/on attribute. belongs_to :company, :touch => true belongs_to :company, :touch => :employees_last_updated_at

Destroy/Remove database in Rails

By issuing rake -T you have the following database tasks: rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config) rake db:drop # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases) rake db:fixtures:load # Load … Read more

Case-insensitive unique index in Rails/ActiveRecord?

Since MySQL indexes are already case-insensitive, I’m guessing you’re dealing with PostgreSQL, which creates case-sensitive indexes by default. I’m answering here based on Rails 3.2.3 and PostgreSQL 8.4. It seems functional indexes are one more example of things that ActiveRecord can’t generate. Foreign keys and UUID columns are two more that come to mind. So … Read more

Upgrading to Rails 6.1.6.1 causes Psych::DisallowedClass: Tried to load unspecified class: Symbol

The error occurs due to the following security update in Rails: https://discuss.rubyonrails.org/t/cve-2022-32224-possible-rce-escalation-bug-with-serialized-columns-in-active-record/81017 The Hash serializer apparently loaded data with YAML.unsafe_load, which now changed to YAML.safe_load. This new method does not handle data types such as Symbol or Time by default for security reasons. The workaround as the announcement suggests is to either migrate the serializer … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)