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 4258490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:39:23+00:00 2026-05-21T05:39:23+00:00

User is a nested attribute of Announcement . When a new announcement is created,

  • 0

User is a nested attribute of Announcement. When a new announcement is created, it will create a new user if its email is not found. Otherwise it should just post announcement into the existing user‘s record.

I am not sure which callback to use before_create or before_save. The following code still does not allow new announcement to be posted into the existing user record. A complete newbie, please help.

class Announcement < ActiveRecord::Base

  attr_accessible :content, :users_attributes 

  has_many :users, :through => :awards

  accepts_nested_attributes_for :users, :reject_if => lambda { |a| a[:email].blank? }, :allow_destroy => true

  before_save :find_user

  private
  def find_user(user)
     If User.find(params[:email]).nil?
       @user = User.new
       @user.save
     else
     @user = User.find(params[:email])
  end
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-05-21T05:39:24+00:00Added an answer on May 21, 2026 at 5:39 am

    You do not need to use any callbacks at all. Rails handles creating and deleting of nested attributes automatically which makes the accepts_nested_attributes_for macro so great. You should not have to manually save, create, or delete except in your controller when you call new and update_attributes. You may be having problems because your awards association should be defined before your users association. In your code sample I don’t see any definition for an awards association even though your users association is through awards. Otherwise I would check your controller and view code. A common problem I have is passing the wrong parameters from the view (make sure you use the fields_for helper in the view).

    http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

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

Sidebar

Related Questions

I have a User and Account models with has_one association and nested attributes. My
I have a nested user control which appears on every single page. It contains
I have a user control nested in a repeater. Inside my user control I
I have a user and nested profile class as follows: class User < ActiveRecord::Base
I need to conditionally load a few nested user controls on a webpage based
I'm finding that in my application, the user can get quite 'nested' in the
I have an Account model that accepts nested attributes for a user model. An
So here's the scenario: User: has_one :company accepts_nested_attributes_for :company Controller: @user = User.new @user.build_company
I am new to Rails just been learning the last month or so. I
Am trying to simplify the create / edit view for a multi-model controller. User

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.