Rails - Create parent and child at the same time in has_one belongs_to


I'm sur I do it wrong, but I can't see where. I've got this two models :

<strong>Subscription.rb (child)</strong>

class Subscription < ActiveRecord::Base attr_accessible :state, :subscriber_id, :subscriber_type, :last_payment belongs_to :subscriber, polymorphic: true validates :subscriber_id, presence: true validates :subscriber_type, presence: true end

<strong>restorer.rb (Parent)</strong>

class Restorer < User attr_accessible :firstname, :lastname, :restaurant_attributes, :subscription_attributes has_one :restaurant, dependent: :destroy, :autosave => true has_one :subscription, as: :subscriber, :autosave => true accepts_nested_attributes_for :restaurant accepts_nested_attributes_for :subscription end

When I want two create a new restorer, and a new subscription (at the same time) It doesn't work :

def create @restorer = Restorer.create params[:restorer] @restaurant = @restorer.build_restaurant params[:restorer][:restaurant_attributes] @subscription = @restorer.build_subscription params[:restorer][:subscription_attributes] if @restorer.save ... else ... end end


Looking at the comments and your code, it looks like the reason it isn't working is because of the validations on Subscriber. Restorer has one Subscription and Subscription belongs to a Subscriber. Nowhere are you creating the subscriber, hence why the subscription is failing it's validation. You need to either remove the validations, or set those validated properties (subscriber_id and subscriber_type) on Subscriber.

It's kinda a bit gross what you are trying to do in your create, but it should look something like this if you are going to do it that way:

def create @restorer = Restorer.create params[:restorer] # These two lines aren't needed if you are accepting nested attributes @restaurant = @restorer.build_restaurant params[:restorer][:restaurant_attributes] @subscription = @restorer.build_subscription params[:restorer][:subscription_attributes] subscriber = Subscriber.new params[:subscriber] @subscription.subscriber = subscriber if @restorer.save ... else ... end end

Btw, its better to validate the relationship subscriber rather than the id subscriber_id:

validates :subscriber, presence: true


  • Ruby error (uninitialized constant User::Relationship)
  • node.js mqtt client using TLS
  • Active record in standalone Ruby
  • Rails 4 - Validate Model without a database
  • Vector push_back only if enough memory is available
  • Gtalk implementation in iOS
  • How to share validations accross models?
  • file_get_contents creating an empty file
  • Swift #available keyword vs respondsToSelector
  • Unity3D loading resources after build
  • How to check if a Firefox WebExtension is installed or not with page JavaScript?
  • Detecting the presence of IDLE / How to tell if __file__ isn't set
  • Checking for a dictionary key which is a range of integers
  • Parent Child SQL Recursion
  • Mongoid same embedded documents types for different fields
  • Handling null values with wso2 CEP
  • How to make Javascript generated checkboxes persist?
  • JSF Cannot format given Object as a Date
  • LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary`2[System.
  • ASP.NET RegularExpressionValidator, validate on a non-match?
  • Get current user from inside the model in Sails
  • Strong vs Weak entities MYSQL
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • Web.config system.webserver errors
  • Force show.bind execution
  • Bad request using file_get_contents for PUT request in PHP
  • Disable Enter in editText android
  • Email format validation in mvc3 view
  • Dynamically accessing properties of knockoutjs observable array
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF