Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

serialized objects not working in view but works in console

#3986
% rails -v
Rails 3.0.0.beta
% ruby -v
ruby 1.9.2dev (2010-02-03 trunk 26544) [x86_64-darwin10.2.0]

% rails g scaffold post data:text
% rake db:migrate

# add serialize to Post
% cat app/models/post.rb
class Post < ActiveRecord::Base
  serialize :data
end

# add class to store in data field
% cat lib/like_a_model.rb 
class LikeAModel
  extend ActiveModel::Naming
  include ActiveModel::Validations
  include ActiveModel::Serialization

  attr_accessor :attributes
  
  def initialize(attributes = {})
    @attributes = HashWithIndifferentAccess.new(attributes)
  end
 
  def read_attribute_for_validation(key)
    @attributes[key]
  end
  
  def to_model
    self
  end

  def valid?()      true end
  def new_record?() true end
  def destroyed?()  true end

  def errors
    obj = Object.new
    def obj.[](key)         [] end
    def obj.full_messages() [] end
    obj
  end
end

% rc
Loading development environment (Rails 3.0.0.beta)
irb(main):001:0> a = LikeAModel.new(:abc => 123, :def => 456)
=> #<LikeAModel:0x00000100edadb8 @attributes={"abc"=>123, "def"=>456}>
irb(main):002:0> b = Post.create(:data => a)
=> #<Post id: 1, data: #<LikeAModel:0x00000100edadb8 @attributes={"abc"=>123, "def"=>456}>, created_at: "2010-02-17 22:10:07", updated_at: "2010-02-17 22:10:07">
irb(main):003:0> x = Post.first
=> #<Post id: 1, data: #<LikeAModel:0x00000102381500 @attributes={"abc"=>123, "def"=>456}>, created_at: "2010-02-17 22:10:07", updated_at: "2010-02-17 22:10:07">
irb(main):004:0> x.data
=> #<LikeAModel:0x00000102381500 @attributes={"abc"=>123, "def"=>456}>
irb(main):008:0> raise x.data.inspect
RuntimeError: #<LikeAModel:0x00000102381500 @attributes={"abc"=>123, "def"=>456}>

everything looking good so far.

now i add x = Post.first; raise x.data.inspect to a view and receive a YAML::Object instead -

RuntimeError in PostsController#index
#<YAML::Object:0x00000100e65270 @class="LikeAModel", @ivars={"attributes"=>{"abc"=>123, "def"=>456}}>

and if i attempt to pass x.data to a form builder i get -

undefined method `model_name' for "LikeAModel":String

Reported by trevor · February 17th, 2010 @ 10:21 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Ryan Bigg
    Ryan Bigg
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 11th, 2010 @ 10:57 AM

  2. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:25 AM

  3. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:48 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:48 PM