Note that there are some explanatory texts on larger screens.

plurals
  1. PONested attributes will not save
    primarykey
    data
    text
    <p>I have searched stackoverflow thoroughly and tried every result that has come up but I am still stuck. Here is the problem.</p> <p>I have an app that allows me to upload music files. Once the file is uploaded, I scan the ID3 data and use that data to populate all the various info for the object in my database....such as title, album, year, genre and so forth. </p> <p>The artist field sometimes has many artists, so for this field I wrote a method that takes all the artists and hashes it as such;</p> <pre><code>{:artist =&gt; [{:name=&gt;"Artist 1"},{:name=&gt;"Artist 2"}]} </code></pre> <p>This data is now included in the object that I want to save to my database and looks as follows;</p> <pre><code>#&lt;Track id: 76, name: nil, title: "song title", tpath: "file_name.mp3", created_at: "2013-06-08 23:31:24", updated_at: "2013-06-08 23:31:24", track_number: 6, artist: {:artist =&gt; [{:name=&gt;"Artist 1"},{:name=&gt;"Artist 2"}]}, album_artist: "Artist Name", year: 2012, genre: "nil", bpm: nil, length: 252, size: 79872, user_id: 2&gt; </code></pre> <p>In my tracks_controller(where all of the action is happening) I have this for my create action;</p> <pre><code>def create @track = Track.new(params[:track]) @track.libraries &lt;&lt; @library @track.parse_id3(@track) @track.update_attributes(@track[:artist]) --&gt;# I also tried @track.artist respond_to do |format| if @track.save format.html { redirect_to library_track_path(@library, @track), notice: 'Track was successfully created.' } format.json { render json: @library_track, status: :created, location: @track } else format.html { render action: "new" } format.json { render json: @track.errors, status: :unprocessable_entity } end end </code></pre> <p>end</p> <p><strong>The problem is that active record is not saving/updating my Artists(in the Artist Table) with the data I am providing, but the track is successfully saved to my database without error.</strong></p> <p>I have the following relationship setup between Artists and Tracks;</p> <pre><code>class Artist &lt; ActiveRecord::Base attr_accessible :age, :birthdate, :first_name, :last_name, :name has_many :tracks, :through =&gt; :artist_tracks has_many :artist_tracks end </code></pre> <p>Join Table looks like;</p> <pre><code>class ArtistTrack &lt; ActiveRecord::Base attr_accessible :album_artist, :artist_id, :track_id belongs_to :artist belongs_to :track end </code></pre> <p>And Track looks like;</p> <pre><code>class Track &lt; ActiveRecord::Base attr_accessible :name, :title, :tpath, :track_number, :artist, :album_artist, :year, :genre, :bpm, :length, :size, :user_id, :artists_attrubutes mount_uploader :tpath, TpathUploader belongs_to :user has_many :libraries, :through =&gt; :track_libraries has_many :track_libraries has_many :playlists, :through =&gt; :playlist_tracks has_many :playlist_tracks has_many :artists, :through =&gt; :artist_tracks has_many :artist_tracks accepts_nested_attributes_for :artists end </code></pre> <p>The only form involved is the form with a single field asking for the location of the file to be uploaded.</p> <p>What am I missing here?</p> <p>Thanks.</p> <hr> <p>I got it to work, here is what was needed.</p> <pre><code>@track.update_attributes(:artists_attributes =&gt; @track.parse_id3(@track)) </code></pre> <p>The <strong>.parse_id3</strong> is a method I wrote that scans all the artist in the MP3's artist field and turns them into;</p> <pre><code>:artists_attributes =&gt; [{:name=&gt;"Artist 1"},{:name=&gt;"Artist 2"}]} </code></pre> <p>I had tried;</p> <pre><code>@track.update_attributes(:artists_attributes) </code></pre> <p>Apparently I needed to add the <strong>@track.parse_id3(@track)</strong> piece for it to work.</p> <p>Thanks for all the responses!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload