...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...
...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...
...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
...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...
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...
...thread terminates: Thread.new do begin User.first # first database access makes a new connection ensure ActiveRecord::Base.clear_active_connections! end end When the Rails process terminates E.g. when you stop the...
...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
Note: ActiveRecord::Base#becomes has a lot of quirks and inconsistent behavior. You probably want to use ActiveType.cast instead. This issue will be encountered when relying on attribute_was methods...
...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.
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...
...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
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...
...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...
...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...
...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...
...in app/data_migration/migration_record.rb. You will need to create an app/data_migration.rb class first. class DataMigration::MigrationRecord < ActiveRecord::Base self.abstract_class = true establish_connection(:data_migration) end 3. Inherit from this class in...
...png on the server, add this to your model (example with Paperclip): class Signature < ActiveRecord::Base HEIGHT = 160 WIDTH = 480 before_save :generate_image has_attached_file :image # options ... private...
...object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.[] (NoMethodError) Background The regular/short cucumber backtrace is not...
...to, don't do it at all. Background Consider these classes: # app/models/user.rb class User < ActiveRecord::Base validate :magic def magic errors.add_to_base('failed') if bad_things? end end ^ # app/models/foo.rb...
...to get virtual attributes with full coercion magic is to use Virtus: class User < ActiveRecord::Base include Virtus.model(:constructor => false) attribute :name, String attribute :age, Integer attribute :birthday, DateTime
...you might find this to be harder than you thought. While you can call ActiveRecord::Base.connection.execute(sql) to execute arbitrary SQL commands, the MySQL connection is configured to only accept...
...ActiveRecord 2.3.x that leads to changes in nested forms getting lost. class Project < ActiveRecord::Base has_many :tasks accepts_nested_attributes_for :tasks end If you access project.tasks after...
...even when an attribute value becomes unassignable. See this example about songs: class Song < ActiveRecord::Base belongs_to :artist belongs_to :record_label assignable_values_for :artist do record_label.artists