ruby on rails - ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection Error -


i have model job, has_many applications, , has_many questions. answer belongs both application , question.

i'm trying make factory method admin can use create applications, without users having write anything.

to this, wrote --

 def self.make_by_admin(params)     app = application.new     app.user_id = params[:user_id]     app.job_id = params[:job_id]      app.questions.each |question|       app.answers.new(question_id: question.id, content: 'n/a')     end      app   end 

but, error

#<activerecord::hasmanythroughcantassociatethroughhasoneormanyreflection: cannot modify association 'application#questions' because source reflection class 'question' associated 'job' via :has_many.> 

what's weird though i'm not modifying questions. want generate blank answers each question.

how go doing that?

full models

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)     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)     job = job.includes(:industries)       .includes(:cities)       .includes(:bullets)       .includes(:roles)       .includes(:questions)      if search       job = job.where('jobs.job_title ?', "%#{search}%")     end      job   end    scope :with_info, ->     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, ->     application        .includes(:job)        .includes(:questions)        .includes(:answers)   end    scope :for_job, ->(job_id)     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 |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 |question|       app.answers.new(question_id: question.id, content: 'n/a')     end      fail     app   end 

end

class question < activerecord::base     belongs_to :job     has_one :answer 

end

class answer < activerecord::base   belongs_to :question   belongs_to :application end 

i ended writing

  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 |id|       app.answers.new(question_id: id, content: 'n/a')     end      app   end 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -