我有一个模型Job,有很多应用程序,还有很多问题。答案属于申请和问题。
我尝试制作一个管理员可以用来创建应用程序的工厂方法,而无需用户编写任何内容。
要做到这一点,我写了 -
def self.make_by_admin(params)
app = Application.new
app.user_id = params[:user_id]
app.job_id = params[:job_id]
app.questions.each do |question|
app.answers.new(question_id: question.id, content: 'N/A')
end
app
end
但是,我收到了错误
#<ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection: Cannot modify association 'Application#questions' because the source reflection class 'Question' is associated to 'Job' via :has_many.>
但奇怪的是,我实际上并没有修改问题。我想做的就是为每个问题生成空白答案。
我该怎么做呢?
class Job < ActiveRecord::Base
default_scope { order('jobs.created_at DESC') }
has_many :bullets, dependent: :destroy, inverse_of: :job
has_many :roles, dependent: :destroy, inverse_of: :job
has_many :questions, dependent: :destroy, inverse_of: :job
has_many :job_city_relations, inverse_of: :job, dependent: :destroy
has_many :cities, through: :job_city_relations
has_many :job_industry_relations, inverse_of: :job, dependent: :destroy
has_many :industries, through: :job_industry_relations
has_many :applications, inverse_of: :job, dependent: :destroy
has_many :users, through: :applications
validates :cities,
:job_title,
:job_summary,
:qualifications,
:industries,
:bullets,
:roles,
:questions,
presence: true
accepts_nested_attributes_for :bullets,
reject_if: :all_blank,
allow_destroy: true
accepts_nested_attributes_for :roles,
reject_if: :all_blank,
allow_destroy: true
accepts_nested_attributes_for :questions,
reject_if: :all_blank,
allow_destroy: true
scope :with_cities, ->(city) do
job = Job
.includes(:industries)
.includes(:cities)
.includes(:questions)
.includes(:bullets)
.includes(:roles)
job = job.where(cities: { id: city }) if city
job
end
scope :with_search, ->(search) do
job = Job.includes(:industries)
.includes(:cities)
.includes(:bullets)
.includes(:roles)
.includes(:questions)
if search
job = job.where('jobs.job_title LIKE ?', "%#{search}%")
end
job
end
scope :with_info, -> do
Job.includes(:industries)
.includes(:cities)
.includes(:bullets)
.includes(:roles)
.includes(:questions)
end
def self.build
job = Job.new
2.times {
job.bullets.build
job.roles.build
}
job.questions.build
job
end
def potentials
good_fits = User.includes(:source, :heat, :applications, common_app: [:cities, :industries])
.where('cities.id IN (?)', self.city_ids)
.where('industries.id IN (?)', self.industry_ids)
.where('users.id NOT IN (?)', self.users.map(&:id))
end
end
class Application < ActiveRecord::Base
STATUS_OPTIONS = ["Application Complete",
"Materials Submitted",
"Pending Interview",
"Second Interview"]
belongs_to :job, counter_cache: true
belongs_to :user, counter_cache: true
has_many :questions, through: :job
has_many :answers, inverse_of: :application, dependent: :destroy
validates :job_id, presence: true
validates :user_id, presence: true
validates :answers, presence: true
accepts_nested_attributes_for :answers,
allow_destroy: true, reject_if: :all_blank
scope :with_dependents, -> do
Application
.includes(:job)
.includes(:questions)
.includes(:answers)
end
scope :for_job, ->(job_id) do
Application
.includes(user: [:source, :heat, common_app: [:cities, :industries]])
.includes(questions: :answer)
.where('applications.job_id = ?', job_id)
end
def self.build(job, appl = Application.new)
job.questions.each do |question|
appl.answers.build(question_id: question.id)
end
appl
end
def self.make_by_admin(params)
app = Application.new
app.user_id = params[:user_id]
app.job_id = params[:job_id]
app.questions.each do |question|
app.answers.new(question_id: question.id, content: 'N/A')
end
fail
app
end
class Question < ActiveRecord::Base
belongs_to :job
has_one :answer
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :application
end
答案 0 :(得分:0)
我最后写了
def self.make_by_admin(params)
app = Application.new
app.user_id = params[:user_id]
app.job_id = params[:job_id]
app.questions.pluck(:id).each do |id|
app.answers.new(question_id: id, content: 'N/A')
end
app
end