...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...
... end # app/models/document/uploader.rb class Document::Uploader < CarrierWave::Uploader::Base ... end # app/models/document.rb class Document < ActiveRecord::Base mount_uploader Document::Uploader end # app/models/contract.rb class Contract < ActiveRecord::Base mount_uploader Contract::Document...
...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...
...we have a Post model and each Post belongs to an author: class Post < ActiveRecord::Base belongs_to :author end To turn a relation of posts into a relation of...
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...
...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...
...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...
...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...
...accept nested attributes for a record that is not an association, e.g.: class Site < ActiveRecord::Base def home_page @home_page ||= Page.find_by_name('home') end does 'accept_nested_attributes...
...Note which belongs to an Author and has a string column category: class Note < ActiveRecord::Base belongs_to :author validates_presence_of :author_id, :category end In your controller you...
...define what should be indexed within Solr from your ActiveRecord models, e.g., class Article << ActiveRecord::Base searchable do text :title end end will make the attribute 'title' of your Article...
...this, simply add a cached_tag_list column to your table. Example: class Company < ActiveRecord::Base acts_as_taggable_on :categories end The cache column has to be named cached...
Note: ActiveRecord::Base#becomes has a lot of quirks and inconsistent behavior. You probably want to use ActiveType.cast instead. ActiveRecord models have with a method becomes(klass) which you can...
Modularity Consider the following example from the Modularity README: # app/models/article.rb class Article < ActiveRecord::Base include DoesStripFields[:name, :brand] end # app/models/shared/does_strip_fields.rb module DoesStripFields as_trait do |*fields| fields.each do...
...modified and might be incorrect. In this case you should reset the sequence (e.g. ActiveRecord::Base.connection.reset_pk_sequence!('sites')) Also see Similar commit in Deskbot
Say you have a User with a Carrierwave attribute #avatar: class User < ActiveRecord::Base mount_uploader :avatar, AvatarUploader end When whitelisting the avatar field in the controller, you might do...
class Document < ActiveRecord::Base scope :any_tags, -> (tags){ where('tags && ARRAY[?]', tags) } scope :all_tags, -> (tags){ where('tags @> ARRAY[?]', tags) } end Document.create(title: "PostgreSQL", tags: ["pg","rails"]) Document.any_tags...
expired_ticket.should_not exist_in_database end end Note that there is also ActiveRecord::Base#destroyed?, but that only checks the receiving instance and does not talk to the...
...are enqueued, they overwrite each other in ResourceController. ActiveRecord - a common practice class Post < ActiveRecord::Base does 'post/behavior' before_validation :do_something end module Post::BehaviorTrait as_trait do
...the next link in the scope chain. Take this class for example: class Meal < ActiveRecord::Base named_scope :for_date, lambda { |date| :conditions => { :date => date }} named_scope :with_meat, :conditions...
...string, especially when doing metaprogramming. A common mistake is something like # WRONG class Page < ActiveRecord::Base %w[main sub].each do |type| belongs_to "#{type}_title" end end # RIGHT
...Page < ActiveRecord::Base %w[main sub].each do |type| belongs_to :"#{type}_title" end end Always convert to a symbol, otherwise you'll have all kinds of fun.
...the uniqueness problem? Another day, another undocumented Rails feature! This time, it’s that ActiveRecord::Base.connection.add_index supports an undocumented option to pass a string argument as the value for...
Starting with active_type 0.4.0 you can use ActiveType.cast for this: class User < ActiveRecord::Base ... end class SignUp < ActiveType::Record[User] ... end user = User.find(1) sign_up = ActiveType.cast(user...