...class nested in another is not supported Assuming an architecture like this: class Building < ActiveRecord::Base end class Building::Room < ActiveRecord::Base belongs_to :building end
...Rails versions, by doing what Rails 7 does, e.g. in your ApplicationRecord. class ApplicationRecord < ActiveRecord::Base def self.invert_where spawn.tap do |relation| relation.where_clause = relation.where_clause.invert end end end
...you would validate presence of parent object id, like in this example: class Parent < ActiveRecord::Base has_many :nested, :inverse_of => :parent accepts_nested_attributes_for :nested end
...ActiveRecord::Base belongs_to :parent validates_presence_of :parent_id # <- end With the parent already persisted creating nesteds still works fine. But one will encounter a 'parent id missing' error...
...would be to handle the cast yourself: class ChangeAnIntegerColumnToString < ActiveRecord::Migration[6.1] class User < ActiveRecord::Base; end def up change_column :users, :address_number, 'varchar USING CAST(rating AS varchar...
...with standard Rails validations, we write a custom validation method like this: class User < ActiveRecord::Base validate :validate_screen_name_is_no_palindrome private def validate_screen_name_is_no...
...below declares a variable @@counter that is shared between all classes of your application: ActiveRecord::Base.class_eval do @@counter = 1 end When encountering such code, Ruby will warn you:
p ActiveRecord::Base.connection.indexes(:table_name...
...transactions for atomic changes, i.e. changes that must happen together or not at all. ActiveRecord::Base.transaction do # Change something # Another, dependent change end Be transparent The script should always state...
...Depending on your problem you might need one or both or those. class Interval < ActiveRecord::Base validates_presence_of :start_date, :end_date # Check if a given interval overlaps this...
EdgeRider 0.3.0 adds support for Rails 4.1 and Ruby 2.1. It forward-ports ActiveRecord::Base.scoped to Rails...
...after the association. As an example, assume you have these two models: class Group < ActiveRecord::Base has_many :users end class User < ActiveRecord::Base validates_presence_of :group_id
group = load_group(group_id) [group_id, render_group_html(group)] ensure ActiveRecord::Base.clear_active_connections # <- close additional database connections end render json: Hash[*group_ids_and_html.flatten(1)] end
...a model User with settings stored as YAML in a single column: class User < ActiveRecord::Base typed_store :settings, coder: YAML do |s| s.string :lang s.integer :show_number_of_posts...
...language. So let's rename the settings key: class RenameLanguageKeyInUser < ActiveRecord::Migration class User < ActiveRecord::Base def rename_settings_key!(from, to) from, to = from.to_s, to.to_s old_settings...
# Vehicle # Vehicle::Car # Vehicle::Bike class MyMigration < ActiveRecord::Migration[6.1] class Vehicle < ActiveRecord::Base def self.find_sti_class(type_name) super(name) end end class Car < Vehicle
...include a final order condition using the primary key of the table. class Photo < ActiveRecord::Base scope :by_date, -> { order('created_at DESC, id DESC') } end Photo.by_date
...use read_attribute(attr_name) and write_attribute(attr_name, value). Example: class Poet < ActiveRecord::Base def name=(name) write_attribute(:name, name.strip) end end See also Why belongs_to/has_many...
...validates_attachment_file_name macro to test the suffix of a filename: class User < ActiveRecord::Base has_attached_file :avatar validates_attachment_file_name :avatar, matches: [/\.png\z/i] end
...a general issue with this pattern/approach. What's happening? Consider these classes: class Post < ActiveRecord::Base belongs_to :thread def thread_title thread.title end end class Thread < ActiveRecord::Base
...is a belongs_to), you probably declared your association incorrectly. Always do class User < ActiveRecord::Base belongs_to :avatar end and never class User < ActiveRecord::Base belongs_to 'avatar'
...user doesn't see a blank application slate on her first visit: class User < ActiveRecord::Base has_many :projects after_create :create_first_project private def create_first_project projects.create...
...contained: [...] the missing elements were: [...] the extra elements were: [...] Expected [...] to contain exactly "XXX" ActiveRecord::Base is monkey patched by super_diff/rspec-rails (includes rspec and rails) super_diff/rails (includes active...
...how to do the upload all manually.) Rails setup Build these models: class Gallery < ActiveRecord::Base has_many :images, dependent: :destroy accepts_nested_attributes_for :images, allow_destroy: true,
...if: :all_blank # Ignore the blank template record end class Image < ActiveRecord::Base belongs_to :gallery mount_uploader :teh_image, YourUploaderHere # Mount Carrierwave on attribute :teh_image end
...second scope will overwrite (and not join!) conditions from the first scope: class Contract < ActiveRecord::Base named_scope :with_region, proc { |region_id| { :conditions => { :region_id => region_id } } } end >> Contract.with...
...The problem in detail Consider this Rails class and the following articles: class Article < ActiveRecord::Base named_scope :available, :conditions => { :state => [ 'unassigned', 'draft' ] } end #<Article id: 1, title: "Cooking noodles...
...use them in a model for example, you have to use the connection: connection = ActiveRecord::Base.connection
...# your SQL Statement results = connection.select_values(sql...