Q:

Rails: Setting associated attribute works in Rails console but not in the app

I have two models in my rails app - Palette and Color. A palette has_many colors. On create action, I want to be able to create a new Palette in the DB with colors. The palette is created in the DB but the colors associated attribute is not being set. Here I need help.
Color model
class Color
  include Mongoid::Document
  field :code,                  :type => String   
  field :image_url,             :type => String

  validates_presence_of :code, :image_url
  validates_uniqueness_of :code

  belongs_to :palette

  class << self
    def add_new(inputs, data = {})
      color = Color.new(inputs)
      color.save!
      color
    end
  end
end

Palette Model
class Palette
  include Mongoid::Document

  field :code,                  :type => String   #P001, P002 etc

  validates_presence_of :code
  validates_uniqueness_of :code

  has_many :colors

  class << self
    def add_new(inputs, data = {})
      new_palette = Palette.new(inputs.except(:colors))
      if inputs[:colors] && inputs[:colors].kind_of?(Array)
        colors = Color.find(inputs[:colors]).to_a
        p colors  # This correctly prints the colors that was  chosen in the form.

        colors.each do |color|
          new_palette.colors << color
        end
      end
      new_palette.save!
      new_palette
    end
  end
end

Below is the server log. The params received are correct. But the Palette object that is created has empty colors []. 

I run the same steps on rails console, the setting of colors of
Palette object works correctly. Not sure what is my mistake here.

Server log
Started POST "/palettes" for 127.0.0.1 at 2014-07-09 16:08:53 +0530
Processing by PalettesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/kbEyysOq5F5tNp1ciOGpMVjhVxuL6nwPgJHijl1yrI=", "palette"=>{"code"=>"PCODE1", "colors"=>["53bbe83f616e690429210000", "53bbea4f616e690429220000"]}}
MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} runtime: 0.5990ms
MOPED: 127.0.0.1:27017 QUERY        database=predicta_webapp_development collection=colors selector={"_id"=>{"$in"=>[BSON::ObjectId('53bbe83f616e690429210000'), BSON::ObjectId('53bbea4f616e690429220000')]}} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 0.9210ms
[#<Color _id: 53bbe83f616e690429210000, code: "SL001", meta: "Blah", image_url: "blah", other_data: {}, palette_id: nil>, #<Color _id: 53bbea4f616e690429220000, code: "SL002", meta: "blah", image_url: "blah", other_data: {}, palette_id: nil>]
MOPED: 127.0.0.1:27017 QUERY        database=predicta_webapp_development collection=palettes selector={"code"=>"PCODE1"} flags=[] limit=-1 skip=0 batch_size=nil fields={:_id=>1} runtime: 0.7580ms
MOPED: 127.0.0.1:27017 INSERT       database=predicta_webapp_development collection=palettes documents=[{"_id"=>BSON::ObjectId('53bd1bbd616e690a6f0c0000'), "other_data"=>{}, "code"=>"PCODE1"}] flags=[]
COMMAND      database=predicta_webapp_development command={:getlasterror=>1, :w=>1} runtime: 1.2380ms
Redirected to http://localhost:3000/

A:

Try saving the color object after you make the association with the palette object. The association just sets the palette_id on the color object but doesn't trigger a save/update.

