ruby on rails - Best practice to #create an instance of Class2 and call logic on it from Class1? -


quoterequest has_one quote belonging_to quoterequest. below tasks i'm trying achieve in rails app:

  1. after quoterequestscontroller#create .saves calls quotescontroller#create (skipping quotescontroller#new because there no view or user input required create quote, internal scrape task).
  2. within quotescontroller#create quote.new instance has quote#gather_quote method called on it
  3. `quote.gather_quote goes on watir crawl , saves values instance variables, successfullly crawls , saves desired values instance variables tested in console.
  4. then quote.save saves instance , it's watir, quote#gather_quote, collected instance variables db. correct columns present in db.

but having trouble with, think, variable passing/scope , or moving quoterequestcontroller#create action quotecontroller#create somewhere in code.

  • quoterequest instances saving fine.
  • quote instances saving, without instance variables create quote#gather_quote. don't think code getting run quote.gather_quote method.

can me out with:

a) finding out what's not working current approach and

b) advising on nicer way of achieving i'm trying if think there one.

thank you.

quote_requests_controller.rb

class quoterequestscontroller < applicationcontroller before_action :authenticate_user!, only: [ :new, :create, :show, :index ]    def new     @company = current_user.company     @quote_request = quoterequest.new   end    def create     @company = current_user.company     @quote_request = @company.quote_requests.build(quote_request_params)     if @quote_request.save       quote.create({ quote_request_id: @quote_request.id} )       render :nothing => true     end   end end 

quote_request.rb

class quoterequest < applicationrecord     belongs_to :company     has_one :quote end 

quote_controller.rb

class quotescontroller < applicationcontroller    def create     @quote = quote.new({ quote_request_id: @quote_request.id })     @quote.get_quote      if @quote.save       render 'show'     end   end    def show     render 'show'   end end 

quote.rb

class quote < applicationrecord   require 'watir'   attr_accessor :lives, :salary, :frequency   attr_reader :url, :username, :password   belongs_to :quote_request    def initialize(args)     @url = 'url' #required watir part of #gather_quote     @username = 'me'     @password = 'password'     super   end     def gather_quote     browser_session     login     start_quote     complete_form     scrape_results   end end 

you seem have fundamentally flawed understanding of controller is.

a controller mechanisms handling single request-response cycle. within single request-response cycle, don't "head over" other controllers. , don't call controllers other controllers. doesn't work way.

further, controllers not tightly coupled models. is, can work (instantiate, update, save, etc.) quote model quoterequestcontroller.

to orchestrate activities across multiple model classes (and stuff crawling external sites), should @ plain old ruby objects (sometimes referred poros). specifically, google around on service objects , service pattern.

finally, don't know how long crawling takes, if it's material amount of time, may need @ background jobs (either avoid request timeouts and/or crap user experience).


Comments

Popular posts from this blog

Add new key value to json node in java -

javascript - Highcharts Synchronized charts with missing data points -

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -