Search code examples
ruby-on-railsentity-relationshipnested-resources

How to pass in parent instance on creation of a nested record in rails console?


I'm mucking around in rails console and I've gotten myself stuck trying to do something pretty simple.

Quote_requests has_one Quote belonging to it.

In the Quote init method I've declared that it needs to take three arguments, none of which is the parent Quote_request. The models are created within rails with the correctly defined relation statements on each side.

I am trying to play around with the objects in rails console to create a new quote on a quote_request, and Quote.new(arg1, arg2, arg3) returns a non initialized quote object;

=> #<Quote:0x007fb64471b978 not initialized>

Because i guess it's breaking the defined relations in the rails app because it should belong_to a Quote_request instance and I've not passed in/connected it to a quote_request instance. I have a quote_request instance all created and ready, but I cannot call;

my_quote_request.quote.new(arg1, arg2, arg3)

Can you help me sort myself out on this simple but fundamental relation, object and message passing exercise.

Thank you


Solution

  • if you defined Quote_request has_one Quote make sure the one that has an "id" is quote, so in you can check from your schema file the model Quote has quote_request_id

    and the way parent(Quote_request) create child(Quote), you can check from this site http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

    as an sample

    @quote_request = QuoteRequest.new
    @quote = @quote_request.create_quote(arg1, arg2, arg 3)