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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:16:59+00:00 2026-06-15T10:16:59+00:00

Let’s imagine I run an imaginary art store with a couple models (and by

  • 0

Let’s imagine I run an imaginary art store with a couple models (and by models I’m referring to the Rails term not the arts term as in nude models) that looks something like this:

class Artwork < ActiveRecord::Base
  belongs_to :purchase
  belongs_to :artist
end

class Purchase < ActiveRecord::Base
  has_many :artworks
  belongs_to :customer
end

The Artwork is created and sometime later it is included in a Purchase. In my create or update controller method for Purchase I would like to associate the new Purchase with the existing Artwork.

If the Artwork did not exist I could do @purchase.artworks.build or @purchase.artworks.create, but these both assume that I’m creating a new Artwork which I am not. I could add the existing artwork with something like this:

params[:artwork_ids].each do |artwork|
  @purchase.artworks << Artwork.find(artwork)
end

However, this isn’t transactional. The database is updated immediately. (Unless of course I’m in the create controller in which case I think it may be done “transactionally” since the @purchase doesn’t exist until I call save, but that doesn’t help me for update.) There is also the @purchase.artwork_ids= method, but that is immediate as well.

I think something like this will work for the update action, but it is very inelegant.

@purchase = Purchase.find(params[:id])
result = @purchase.transaction do
  @purchase.update_attributes(params[:purchase])
  params[:artwork_ids].each do |artwork|
    artwork.purchase = @purchase
    artwork.save!
  end
end

This would be followed by the conventional:

if result 
  redirect_to purchase_url(@purchase), notice: 'Purchase was successfully updated.' }
else
  render action: "edit"
end

What I’m looking for is something like the way it would work from the other direction where I could just put accepts_nested_attributes_for in my model and then call result = @artwork.save and everything works like magic.

  • 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-15T10:17:00+00:00Added an answer on June 15, 2026 at 10:17 am

    I have figured out a way to do what I want which fairly elegant. I needed to make updates to each part of my Product MVC.

    Model:

    attr_accessible: artwork_ids
    

    I had to add artwork_ids to attr_accessible since it wasn’t included before.

    View:

    = check_box_tag "purchase[artwork_ids][]", artwork.id, artwork.purchase == @purchase
    

    In my view I have an array for each artwork with a check_box_tag. I couldn’t use check_box because of the gotcha where not checking the box would cause a hidden value of “true” to be sent instead of an artwork id. However, this leaves me with the problem of deleting all the artwork from a purchase. When doing update, if I uncheck each check box, then the params[:purchase] hash won’t have an :artwork_ids entry.

    Controller:

    params[:purchase][:artwork_ids] ||= []
    

    Adding this guarantees that the value is set, and will have the desired effect of removing all existing associations. However, this causes a pesky rspec failure
    Purchase.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) fails because :update_attributes actually received {"these"=>"params", "artwork_ids"=>[]}). I tried setting a hidden_value_tag in the view instead, but couldn’t get it to work. I think this nit is worthy of a new question.

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

Sidebar

Related Questions

Let me try to explain by example. Say website is hosted at example.com (NOT
Let's say I have a simple form in the administration of my Rails 2.x
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Let's have an example like below: package xliiv.sandbox; import android.app.Activity; import android.os.Bundle; import android.util.Log;
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let's say I have a table with a Color column. Color can have various
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords

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.