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:
- after
quoterequestscontroller#create.saves callsquotescontroller#create(skipping quotescontroller#new because there no view or user input required create quote, internal scrape task). - within
quotescontroller#createquote.newinstance hasquote#gather_quotemethod called on it - `quote.gather_quote goes on watir crawl , saves values instance variables, successfullly crawls , saves desired values instance variables tested in console.
- then
quote.savesaves 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
Post a Comment