我的rails应用程序中有Company模型。其中包含以下字段:
class Company < ApplicationRecord
include Validatable
validates :contact_one, :city, :state, :country, :address, presence: true
validates :email, presence: true, uniqueness: true
has_many :company_domains
has_many :domains, through: :company_domains
has_many :job_posts
has_many :contacts
has_many :images, as: :imageable
belongs_to :industry, optional: true, counter_cache: true
belongs_to :user, optional: true
def pending_approval?
self.status == 'in-review'
end
def approved?
self.status == 'approved'
end
end
# == Schema Information
#
# Table name: companies
#
# id :integer not null, primary key
# address :string
# city :string
# company_domains_count :integer
# company_size :string
# contact_one :string
# contact_two :string
# contacts_count :integer
# country :string
# cover_image :string
# email :string
# facebook_url :string
# fax :string
# google_plus_url :string
# job_posts_count :integer
# latitude :float
# linkedin_url :string
# logo_image :string
# longitude :float
# name :string
# state :string
# status :string
# twitter_url :string
# website :string
# year_of_establishment :date
# youtube_url :string
# created_at :datetime not null
# updated_at :datetime not null
# industry_id :integer
# user_id :integer
当我在Rails控制台中检查 Company.instance_methods(false)时,我得到了以下一系列实例方法:
[:after_add_for_images?, :after_add_for_images=, :before_remove_for_images, :before_remove_for_images?, :before_remove_for_images=, :after_remove_for_images, :after_remove_for_images?, :after_remove_for_images=, :belongs_to_counter_cache_after_update, :autosave_associated_records_for_industry, :autosave_associated_records_for_user, :autosave_associated_records_for_company_domains, :validate_associated_records_for_company_domains, :before_add_for_company_domains, :before_add_for_company_domains?, :before_add_for_company_domains=, :after_add_for_company_domains, :after_add_for_company_domains?, :after_add_for_company_domains=, :before_remove_for_company_domains, :before_remove_for_company_domains?, :before_remove_for_company_domains=, :after_remove_for_company_domains, :after_remove_for_company_domains?, :after_remove_for_company_domains=, :autosave_associated_records_for_domains, :validate_associated_records_for_domains, :before_add_for_domains, :before_add_for_domains?, :before_add_for_domains=, :after_add_for_domains, :after_add_for_domains?, :after_add_for_domains=, :before_remove_for_domains, :before_remove_for_domains?, :before_remove_for_domains=, :after_remove_for_domains, :after_remove_for_domains?, :after_remove_for_domains=, :autosave_associated_records_for_job_posts, :validate_associated_records_for_job_posts, :before_add_for_job_posts, :before_add_for_job_posts?, :before_add_for_job_posts=, :after_add_for_job_posts, :after_add_for_job_posts?, :after_add_for_job_posts=, :before_remove_for_job_posts, :before_remove_for_job_posts?, :before_remove_for_job_posts=, :after_remove_for_job_posts, :after_remove_for_job_posts?, :after_remove_for_job_posts=, :autosave_associated_records_for_contacts, :validate_associated_records_for_contacts, :before_add_for_contacts, :before_add_for_contacts?, :before_add_for_contacts=, :after_add_for_contacts, :after_add_for_contacts?, :after_add_for_contacts=, :before_remove_for_contacts, :before_remove_for_contacts?, :before_remove_for_contacts=, :after_remove_for_contacts, :after_remove_for_contacts?, :after_remove_for_contacts=, :autosave_associated_records_for_images, :validate_associated_records_for_images, :before_add_for_images, :before_add_for_images?, :before_add_for_images=, :after_add_for_images]
有人可以帮助我了解这些方法的作用吗?
答案 0 :(得分:1)
这些是来自
的关联回调关联回调正常的回调连接到 Active Record对象,使您可以在以下位置使用这些对象 各方面。例如,您可以使用:before_save回调来 导致在保存对象之前发生某些事情。
关联回调与普通回调类似,但它们是 由集合生命周期中的事件触发。有四个 可用的关联回调:
before_add after_add before_remove after_remove
答案 1 :(得分:0)
我猜您在使用某种宝石来处理图像上传吗?看起来这些是图像上传的挂钩,因此您可以在添加/删除图像之前/之后执行操作。这将类似于活动记录挂钩,例如before_save等
我将参考您所使用的宝石的文档。