i have free standing question model in rails app questions loaded via yaml. find right question , load view creating salequalifier (which has question_id field), have not declared association (e.g. salequalifier has_one question) because breaks other parts of site.
now i'm looking find salequalifiers in app depending on fields in both salequalifier , question models - , i'm trying write correct sql query can retrieve records. can me understand how write effectively, first foray writing own sql commands.
my schema:
create_table "questions", force: true |t| t.text "question_text" t.string "answer_type" t.integer "next_question_id_yes" t.integer "next_question_id_no" t.datetime "created_at" t.datetime "updated_at" t.string "sale_stage" t.boolean "collectable", default: false end create_table "sale_qualifiers", force: true |t| t.integer "question_id" t.integer "next_question_id", default: 1 t.datetime "created_at" t.datetime "updated_at" t.integer "sales_opportunity_id" t.boolean "has_answer", default: false t.integer "prior_question_id", default: 1 t.integer "collection_id" end
my question.rb:
class question < activerecord::base validates :next_question_id_yes, :question_text, :answer_type, presence: true file.open("#{rails.root}/config/initializers/questions.yml", 'r') |file| yaml::load(file).each |record| question.create(record) unless question.exists?(record) end end end
salequalifier.rb:
class salequalifier < activerecord::base has_one :answer, :inverse_of => :sale_qualifier, dependent: :destroy accepts_nested_attributes_for :answer belongs_to :sales_opportunity validates :sales_opportunity_id, presence: true validates_associated :answer after_save :update_sales_opp_question end
an example question:
- id: 1 question_text: why company need buy products or services? answer_type: text field next_question_id_yes: 2 next_question_id_no: ~ collectable: ~ sale_stage: prospecting
what i'm attempting achieve find salequalifiers belong_to sales_opportunity, have has_answer: 'true', , have question_id question sale_stage prospecting.
what i've tried:
salequalifier.find_by_sql("select * sale_qualifiers sales_opportunity_id = '83' has_answer = 'true' inner join questions on questions.id = sale_qualifiers.question_id order question.id desc")
which results in following error:
salequalifier load (15.8ms) select * sale_qualifiers inect * sale_qualifiers sales_opportunity_id = '83' has_answer = 'true' inner join questions on questions.id = sale_qualifiers.question_id order question.id desc pg::error: error: syntax error @ or near "*" line 1: select * sale_qualifiers inect * sale_qualifiers w...
can me understand how write query please?
try
salequalifier.find_by_sql("select sale_qualifiers.* sale_qualifiers sales_opportunity_id = '83' has_answer = 'true' inner join questions on questions.id = sale_qualifiers.question_id order question.id desc")
Comments
Post a Comment