rails-activerecord

Create or override Rails Active Record macros

独自空忆成欢 提交于 2019-12-07 21:24:42
问题 In a Rails app, Active Record creates created_at and updated_at columns thank to macros, (it seems to be also called "magic columns"). See Active Record Migrations I have some questions about that mecanism: Is it possible to override that to get a third column (e.g. deleted_at ) ? Is it possible to create a new macro t.publishing that will create publish_up and publish_down columns, for example? And where to code that? Obviously, I know I can add those columns manually, but I wonder how to

string vs text using Rails 3.2.* and Postgres - should I always just use text

陌路散爱 提交于 2019-12-07 18:16:44
问题 I adopted a Rails app (Rails 3.2 and Postgres 9.4) that has a few Rails strings and we have gone past the 255 limit. This app had previously used MySQL rather than Postgres as backing store. My understanding is that postgres handles strings and text the same. Is this correct? Are there any limitations that we should be aware of before migrating all our Rails strings to texts? Issues of performance are a bit of a concern but not the dominant concern. 回答1: From the fine manual: Tip : There is

Unable to autoload constant ActiveStorage::Blob::Analyzable Error with Rails 5.2, AWS S3, and ActiveStorage

醉酒当歌 提交于 2019-12-07 14:47:41
问题 I've been battling this guy for a while and have done all the Googlies on it (here, here, and many equally-unhelpful others) but to no avail. The official error is this, called on the first line of my create method: Unable to autoload constant ActiveStorage::Blob::Analyzable, expected /Users/lizbayardelle/.rvm/gems/ruby-2.5.0/gems/activestorage-5.2.1/app/models/active_storage/blob/analyzable.rb to define it I'm creating a blog model, which has_one_attached :pdf and one :image , both through

next available record id

本秂侑毒 提交于 2019-12-07 10:01:18
问题 @user = User.new @user.id returns nil but i need to know it before i save. Is it possible ? 回答1: No, you can't get the ID before saving. The ID number comes from the database but the database won't assign the ID until you call save . All this is assuming that you're using ActiveRecord of course. 回答2: YES you can! I had the same question and investigated the docs. The ability to solve this question is very related to your database type in fact. Oracle and Postgresql do have useful functions to

How to integrate :missed days with :committed days in habits.rb?

早过忘川 提交于 2019-12-07 09:09:29
How can we integrate t.integer :missed with t.text :committed so that when a User checks off he :missed 3 :committed days in a :level he has to restart the :level ? for each :missed day he checks off, an additional :committed day is added back into the :level so that he must make it up before advancing? Each habit has 5 levels before "Mastery" is achieved! class Habit < ActiveRecord::Base belongs_to :user before_save :set_level acts_as_taggable serialize :committed, Array def self.comitted_for_today today_name = Date::DAYNAMES[Date.today.wday].downcase ids = all.select { |h| h.committed

Rails HABTM after_add callback fires before saving primary object

筅森魡賤 提交于 2019-12-07 09:08:22
问题 I have two ActiveRecord models having a HABTM relationship with eachother. When I add an AccessUnit through a form that allows zones to be added by checking checkboxes I get an exception that the AccessUnitUpdaterJob can't be enqueued because the access unit passed can't be serialized (due to the fact that the identifier is missing). When manually calling save on the primary object, the issue is resolved but of course this is a workaround and not a proper fix. TLDR; it seems the after_add

rails validation of nested attributes for uniqueness when some may be marked for destroy

大憨熊 提交于 2019-12-07 04:42:44
问题 I've got the following (sanitized) models: class Person < ActiveRecord::Base attr_accessible :name, :first_name, :last_name, :age, :job_title, :salary, :ssn, :prison_convictions, :addresses_attributes has_many :addresses, inverse_of: :person accepts_nested_attributes_for :addresses, allow_destroy: true end class Address < ActiveRecord::Base attr_accessible :zip_code, :street,:house_number, :unique_per_person_government_id belongs_to :person, inverse_of: :addresses validates_uniqueness_of

Odd rake db:migrate output

独自空忆成欢 提交于 2019-12-07 03:52:23
问题 Why does rake db:migrate run Execute db:schema:dump my output is all screwed up (showing SQL). Looks like this: ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations" (3.7ms) SELECT t2.oid::regclass::text AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete FROM pg_constraint c JOIN pg_class t1 ON c.conrelid = t1.oid JOIN pg_class t2 ON c.confrelid = t2.oid JOIN pg

Why does ActiveRecord in Rails not support Multiple Table Inheritance?

时光毁灭记忆、已成空白 提交于 2019-12-07 00:37:45
问题 I was trying to implement a set of models I had put together on paper and ran into an issue where I thought the best way to go about it would be using a Multiple Table Inheritance setup. However, after google searching I found that ActiveRecord doesn't support MTI... even though there are plenty of articles showing how it can be done. This had me wondering if I was setting up my models correctly if it's not implemented. So my question is why doesn't Active Record have built in support for MTI

Is it possible to define composite primary key for table using active record? [duplicate]

旧巷老猫 提交于 2019-12-06 20:17:37
问题 This question already has answers here : How to set composite key in Rails application (2 answers) Closed 5 years ago . I'm using ActiveRecord in a ruby project without rails. I need to define composite primary key for a table.Normally migration creates primary key automatically. Is it possible to define my own composite primary key for a table using active record? 回答1: There are some gems that give you this ability, such as composite_primary_key. I don't know if Rails 4 has added support for