How to Configure Columns That Do Not Match Naming Conventions in ActiveRecord

Asked 2 years ago, Updated 2 years ago, 56 views

Thank you for your help.
Tell me about ActiveRecord.

ActiveRecord automatically creates relationships and inserts data according to the column naming convention, but how do I set up columns when I want to use an existing database?

For example, to apply created_at to the column created.

Also, could you tell me the relevant search words that I can use to look up this information?

Thank you for your cooperation.

Note:

This time, I had to modify the data, and I thought it would be easy to program using ActiveRecord, so I came up with this kind of method.This is a one-time program, so I would like to do it without tampering with the columns.

rails-activerecord

2022-09-30 10:30

2 Answers

I looked it up and found that using the alias_attribute method is good (although I have never used it).

class Movie <ActiveRecord::Base
  alias_attribute:created_at,:created
end

However, created_at is a special column, so the above method may not work.
There are many ways to hack on the page below.

But I haven't tried this again, and the information is a little old, so I don't know if it works with recent Rails.

Generally speaking, Rails are most productive when they are "on the rails," or when they are created according to Rails rules.
As your question will be "off the rails", we recommend that you adjust your implementation to Rails as much as possible without relying too much on these hacks.

By the way, the search keyword I used this time looks like this.

  • activerecord override column name
  • activerecord alias name created_at

For your information.


2022-09-30 10:30

Here, we write directly in the model class, but if there are many, it might be better to inherit them appropriately.

class User<ActiveRecord::Base
  before_create —set_created
  before_save —set_updated

  private

  default_created
    self.created=DateTime.now
  end

  default_updated
    self.updated=DateTime.now
  end
end
class User<ActiveRecord::Base
  private

  def timestamp_attributes_for_update
    super+[:updated]
  end

  def timestamp_attributes_for_create
    super+[:created]
  end
end

If you want to change for all models, you may want to overwrite the ActiveRecord::Timestamp method.Specifically, the config/initializers file below.(The filename is optional.)

#config/initializers/set_activerecord_timestamps.rb
module ActiveRecord
  module Timestamp
    def timestamp_attributes_for_update
      [:updated_at,:updated_on,:updated]
    end

    def timestamp_attributes_for_create
      [:created_at,:created_on,:created]
    end
  end
end

cf.https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/timestamp.rb

You can also change the ActivaRecord::Base directly.(You should be able to use before_*** or override the method.) You should also consider changing the DB column name to match Rails.


2022-09-30 10:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.