...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'
...With ActiveRecord you could implement it with a single belongs_to association: class Node < ActiveRecord::Base belongs_to :parent, class: 'Node' end Any modification of the tree (like adding a...
p ActiveRecord::Base.connection.indexes(:table_name...
...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
...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
...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:
...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
...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...
...gem 'has_defaults' gem 'json-schema' db/migrate/20190511155410_add_analytic_stats_to_projects.rb class AddRepositoryStatsToProjects < ActiveRecord::Migration[5.2] class Project < ActiveRecord::Base end def up add_column(:projects, :analytic_stats, :jsonb) Project.reset_column_information Project.find_each...
...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...
...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...
...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
...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...
...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...
...single INSERT statement describing multiple rows. In Rails 6+ you can do so with ActiveRecord::Base.insert_all. This is very fast, but you won't get the usual guarantees from...
This traverses up the hierarchy until it encounters either a class inheriting from ActiveRecord::Base or a class inheriting from an abstract class (inheriting from ActiveRecord::Base...
...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...
...attachment, you would like to highlight it with errors from any attribute: class Note < ActiveRecord::Base has_attached_file :attachment validates_attachment_presence :attachment validates_format_of :attachment_file_name...
...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...
...implement two methods to synchronize the string array with its underlying association: class Note < ActiveRecord::Base include DoesListField[:topic_list] private def read_topic_list topics.collect(&:name) end
...to use the :integer option so elements get casted to numbers automagically: class Note < ActiveRecord::Base include DoesListField[:author_list, integer: true]
...the following script which is only run once, manually, via script/runner: # lib/scripts/doomsday.rb class User < ActiveRecord::Base; end User.destroy_all In that case we would define a User model solely for...
...attributes to put into po files. If it encounters any declaration like ... class User < ActiveRecord::Base ... it will claim that it can handle the class. Then, its parse method will...
...callbacks the sequential arrangement of your code may be important. For example: class Container < ActiveRecord::Base before_destroy :a_callback has_many :items, :dependent => :destroy end results in container.destroy
# => container.items.destroy_all but class Container < ActiveRecord::Base has_many :items, :dependent => :destroy before_destroy :a_callback end ends up in container.destroy # => container.items.destroy_all # => a_callback Furthermore: Issues when destroying...
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
options = { settings: { number_of_replicas: NUMBER_OF_REPLICAS}, } searchick **options ... end Class Foo < ActiveRecord::Base include DoesSearch ... Our operations team used to patch the number_of_replicas setting manually...