How to Properly Create a Associated Record Just After the Associator Record Creation

How to properly create a associated record just after the associator record creation?

You are looking for something along the lines of...

Class Article

has_many :comments

after_create :create_first_comment!

def create_first_comment!
comments.create
end

end

Associating two records after create in Rails

Where @user is a recently created user, and @person is an existing person.

@user.person = @person
@user.save

Alternately:

User.new :person => @person, ... #other attributes

or in params form:

User.new(params[:user].merge({person => @person}))

As far as forms go:

<% form_for @user do |f| %>
...
<% fields_for :person do |p| %>
<%= p.collection_select, :id, Person.all, :id, :name, :include_blank => "Use fields to create a person"%>
<%= p.label_for :name%>
<%= p.text_field :name %>
...
<% end %>
<% end %>

And in the user controller:

def create
@user = User.create(params[:user])
@person = nil
if params[:person][:id]
@person = Person.find(params[:person][:id])
else
@person = Person.create(params[:person])
end
@user.person = @person
...
end

Create associated records when creating a user with Devise taking an attribute for the associated record

I think that you made several mistakes in all kind of places. Please replace your code with my correction below (so not to miss singular/plural cases). Unfortunately, I can't really test my code, but I think it is pretty close to what your need.

class User < ActiveRecord::Base
has_many :memberships
has_many :organizations, through: :memberships

accepts_nested_attributes_for :memberships
end

class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :organization

accepts_nested_attributes_for :organization
end

class Organization < ActiveRecord::Base
has_many :memberships
has_many :users, through: :memberships
end

= simple_for_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
= devise_error_messages!
= f.simple_fields_for :memberships do |fm|
= fm.input :kind, as: :hidden, input_html: {value: Membership::OWNER}
= fm.simple_fields_for :organization do |fo|
= fo.input :name, required: false, label: "Organization"
= f.input :name
= f.input :email
= f.input :password
= f.input :password_confirmation

def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up) do |u|
u.permit(:email,
:password,
:password_confirmation,
memberships_attributes: [:id, organization_attributes: [:id, :name]])
end
end

Here is how I would handle your view (instead of building your resouces in the new action). You can remove your buildings in that new action and try this view:

= simple_for_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
= devise_error_messages!
= f.object.memberships.build if f.object.memberships.empty?
= f.simple_fields_for :memberships do |fm|
= fm.input :kind, as: :hidden, input_html: {value: Membership::OWNER}
= fm.object.build_organization unless fm.object.organization
= fm.simple_fields_for :organization do |fo|
= fo.input :name, required: false, label: "Organization"
= f.input :name
= f.input :email
= f.input :password
= f.input :password_confirmation

Rails - has_many :through - Create new record and association in one form

I'll write this from a somewhat general perspective (but using the models you described), because it is not just relevant to the situation you described, but relevant to any time you're creating a new association in a Many-to-Many relationship using has_many :through.

In rails, here is a simple example to create a new Topic object, which is associated with a course:

@course.topics << Topic.new(params[:topic])

The above assumes you have previously already loaded up your Course object and stored it in @course. It also assume that the Topic data is coming from a form, and stored in the parameter map under the :topic key.

If you examine your log when this portion executes, because you set up your associations correctly, you should see two insert statements: INSERT INTO "topics"... and INSERT INTO "contents"....

There are other ways (some far more roundabout than others) to do this, but I believe this is the most straightforward.

Let me know if that makes sense.

Rails: How to create a new record in a belongs_to + has_many relationship with both ends validating the presence of each other?

can u try something like this?

game = Game.new
manufacturer = Manufacturer.new(games: [game])
manufacturer.save

and turn on

class Manufacturer < ActiveRecord::Base
has_many :games, autosave: true
# etc etc
end


Related Topics



Leave a reply



Submit