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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:55:29+00:00 2026-05-25T01:55:29+00:00

I’m trying to get my head around saving to multiple models from the one

  • 0

I’m trying to get my head around saving to multiple models from the one controller and it’s probably really simple but I can’t figure it out.

I have a User Model that has many loanitems and the loanitems belong to the user associations set up.

In my loanitems controller I want each loanitem create action to update a the user.points

So at the moment I have the following code that doesn’t throw any errors but doesn’t update the user model either.

def create
    @loanitem = current_user.loanitems.build(params[:loanitem])
    respond_to do |format|
      if @loanitem.save
        @loanitem.user.points = @loanitem.user.points + 50
        @loanitem.user.save
        format.html {redirect_to root_path, :flash => {:success => "Loan Item created" } }
        format.xml{render xml: root_path}
      else
        format.html {render 'pages/home' }
        format.xml {render xml: 'pages/home'}
      end
    end
  end

I’m also trying the following variation on a theme

  def create
    @loanitem = current_user.loanitems.build(params[:loanitem])
    respond_to do |format|
      if @loanitem.save
        current_user.points = current_user.points + 50
        current_user.save
        format.html {redirect_to root_path, :flash => {:success => "Loan Item created" } }
        format.xml{render xml: root_path}
      else
        format.html {render 'pages/home' }
        format.xml {render xml: 'pages/home'}
      end
    end
  end

But should I be sending some message instead to the userupdate controller instead? That currently looks like this …

def update
  @user = User.find(params[:id])
  if
    @user.update_attributes(params[:user])
    redirect_to @user, :flash => { :success => "Profile has been updated!"}
  else
    @title = "Edit Profile"
    render 'edit'
  end
end

Or I have heard that the business logic really should all be contained in the model so maybe the method should be written in User.rb and then called by the Loanitems controllers create method?

I know it’s a real rookie question but any advice would be really welcome.

  • 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-25T01:55:30+00:00Added an answer on May 25, 2026 at 1:55 am

    It sounds like you need to use a Transaction, so you can modify multiple items as a single atomic unit:

    def create
        respond_to do |format|
          User.transaction do
            begin
              @loanitem = current_user.loanitems.create!(params[:loanitem]) # raises exception if it can't create
              @loanitem.user.update_attributes!(:points => @loanitem.user.points + 50) # raises exception if it can't update
    
              format.html {redirect_to root_path, :flash => {:success => "Loan Item created" } }
              format.xml{render xml: root_path}
    
            rescue ActiveRecord::RecordInvalid
              format.html {render 'pages/home' }
              format.xml {render xml: 'pages/home'}
    
              raise ActiveRecord::Rollback
            end
          end
        end
      end
    

    This allows you to have a simple happy-path where multiple objects are updated/created and roll-back all changes so far if anything goes wrong and render your error handling logic. The objects will have the validation messages you can display to the user.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.