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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:07:15+00:00 2026-05-23T07:07:15+00:00

I use Rails 3.0.6 with mongoID 2.0.2. Recently I encountered an issue with save!

  • 0

I use Rails 3.0.6 with mongoID 2.0.2. Recently I encountered an issue with save! method when overriding setter (I am trying to create my own nested attributes).

So here is the model:

class FeedItem
  include Mongoid::Document
  has_many :audio_refs

  def audio_refs=(attributes_array, binding)

    attributes_array.each do |attributes|
      if attributes[:audio_track][:id]
        self.audio_refs.build(:audio_track => AudioTrack.find(attributes[:audio_track][:id]))
      elsif attributes[:audio_track][:file]
        self.audio_refs.build(:audio_track => AudioTrack.new(:user_id => attributes[:audio_track][:user_id], :file => attributes[:audio_track][:file]))
      end
    end

    if !binding
      self.save!
    end
  end

AudioRef model (which is just buffer between audio_tracks and feed_items) is:

class AudioRef
  include Mongoid::Document

  belongs_to :feed_item
  belongs_to :audio_track

end

And AudioTrack:

class AudioTrack
  include Mongoid::Document
  has_many :audio_refs
  mount_uploader :file, AudioUploader
end

So here is the spec for the FeedItem model which doesn`t work:

  it "Should create audio_track and add audio_ref" do
      @audio_track = Fabricate(:audio_track, :user_id => @author.id, :file => File.open("#{Rails.root}/spec/stuff/test.mp3"))
      @feed_item= FeedItem.new(
        :user => @author,
        :message => {:body => Faker::Lorem.sentence(4)},
        :audio_refs => [
            {:audio_track => {:id => @audio_track.id}},
            {:audio_track => {:user_id => @author.id, :file => File.open("#{Rails.root}/spec/stuff/test.mp3")}}
        ]
      )
      @feed_item.save!          
      @feed_item.reload
      @feed_item.audio_refs.length.should be(2)
  end

As you can see, the reason I am overriding audio_refs= method is that FeedItem can be created from existing AudioTracks (when there is params[:audio_track][:id]) or from uploaded file (params[:audio_track][:file]).

The problem is that @feed_item.audio_refs.length == 0 when I run this spec, i.e. audio_refs are not saved. Could you please help me with that?

Some investigation:

1) binding param is “true” by default (this means we are in building mode)

  • 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-23T07:07:16+00:00Added an answer on May 23, 2026 at 7:07 am

    I found a solution to my problem but I didnt understand why save method doesnt work and didn`t make my code work. So first of all let me describe my investigations about the problem. After audio_refs= is called an array of audio_refs is created BUT in any audio_ref is no feed_item_id. Probably it is because the feed_item is not saved by the moment.

    So the solution is quite simple – Virtual Attributes. To understand them watch corresponding railscasts

    So my solution is to create audio_refs by means of callback “after_save”

    I slightly changed my models:

    In FeedItem.rb I added

    attr_writer :audio_tracks #feed_item operates with audio_tracks array
    after_save :assign_audio #method to be called on callback
    
    def assign_audio
        if @audio_tracks
          @audio_tracks.each do |attributes|
            if attributes[:id]
              self.audio_refs << AudioRef.new(:audio_track => AudioTrack.find(attributes[:id]))
            elsif attributes[:file]
              self.audio_refs << AudioRef.new(:audio_track => AudioTrack.new(:user_id => attributes[:user_id], :file => attributes[:file]))
            end
          end
        end
      end
    

    And the spec is now:

    it "Should create audio_track and add audio_ref" do
          @audio_track = Fabricate(:audio_track, :user_id => @author.id, :file => File.open("#{Rails.root}/spec/stuff/test.mp3"))
          @feed_item= FeedItem.new(
            :user => @author,
            :message => {:body => Faker::Lorem.sentence(4)},
            :audio_tracks => [
                {:id => @audio_track.id},
                {:user_id => @author.id, :file => File.open("#{Rails.root}/spec/stuff/test.mp3")}
            ]
          )
          @feed_item.save!          
          @feed_item.reload
          @feed_item.audio_refs.length.should be(2)
      end
    

    And it works fine!!! Good luck with your coding)

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

Sidebar

Related Questions

i'm trying to use Mongoid with rails 3.2.2, i've added: gem mongoid, ~> 2.4
I've been trying to use Mongoid with Rails on Ubuntu. I have installed mongoDB
i use rails and mongodb (mongoid gem). i need to create a select form
I'm trying to use the jQuery token input plugin with Rails and Mongoid. Using
I'm trying to use the Mongoid / Devise Rails 3.1 template ( Mongoid and
I'm trying to use rails nested form_for helper, but I am getting the following
I'm trying to use Rails validations for a form text box to see if
I recently played with MongoDB in Rails using Mongoid . I like the ability
I'm in the process of converting my Rails app to use mongodb through mongoid.
I was writing a Blog like application Using Rails3/Mongoid, and now trying to use

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.