Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8752089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:10:04+00:00 2026-06-13T13:10:04+00:00

I can create a record with the correct association using the create method, but

  • 0

I can create a record with the correct association using the create method, but if I use build then save the instance, It doesn’t create the association.

this works

@account = Account.find(params[:id])  
@user = @account.users.create!(:profile_attributes => { name:  name, company: company_name },email: email, password: password, password_confirmation: password)

but this will only create the user and not the association to the account, which is through a polymorphic membership model

@account = Account.find(params[:id])  
@user = account.users.build(:profile_attributes => { name:  name, company: company_name },email: email, password: password, password_confirmation: password)
@user.save

I want to use save so that I can use all the validations and callbacks on this.

membership.rb

class Membership < ActiveRecord::Base

  belongs_to :target, polymorphic: true
  belongs_to :user
  belongs_to :team

  validates :target, presence: true
  validate  :has_user_or_team

  module HasMembersMixin
    extend ActiveSupport::Concern

    included do
      has_many :memberships,  as: :target
      has_many :users, through: :memberships
    end
    module ClassMethods
      def accessible_by(user)
        conditions = Membership.arel_for_user_or_their_teams(user)
        if direct_conditions = directly_accessible_by(user)
          conditions = conditions.or(direct_conditions)
        end
        includes(:memberships).where conditions
      end
    end
end

module methods excluded

class Account < ActiveRecord::Base
   include Membership::HasMembersMixin
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:10:06+00:00Added an answer on June 13, 2026 at 1:10 pm

    Ah, now I realized. So, the thing is, when you create an AR instance, the associations which are not stored will be all saved. This is the default creation behaviour: save everything. But, if a record already exists, the changes on its associations made using it will not persist. Let’s say an account as a user, and this is an example:

    Account.new(:user => User.new) # this saves the account and the user
    a = Account.find(params[:id]); a.user.name = "Boris Karloff" ; a.save # this will not store the user name
    

    So, this is default AR behaviour, there’s not much you can do. You can set :autosave => true on the association, but I wouldn’t recommend it (each time you would save an account, it would also always try to save all users, even though you hadn’t made any changes to them). It’s, let’s just say, a feature bug 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using Redis in order to create a record you can create a hash
TPath record has class constructor TPath.Create which initialize class vars. But, I can't find
I would like to create a list of records. I can add one record
I can create and use dynamic two dimensional array in Fortran (in 77 standard).
I can create database manually by going to cpanel. But I'd like to create
I can create a contact that is not mail enabled, but how do I
probably a silly question, but I'm not sure how to handle this 'correct'. I
I have this relationship where User can create a document(trip) and invite other users
You can create a table model and add it to a table TableModel tm
How can create a string in JSON after a mysql select: $ritorno = '{Prodotto:'.$riga['Prodotto'].',Prezzo:'.$riga['Prezzo'].'}'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.