This project is archived and is in readonly mode.

#3986 ✓stale
trevor

serialized objects not working in view but works in console

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

% 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

Comments and changes to this ticket

  • Ryan Bigg

    Ryan Bigg October 11th, 2010 @ 10:57 AM

    • Importance changed from “” to “Low”

    Automatic cleanup of spam.

  • Ryan Bigg

    Ryan Bigg October 19th, 2010 @ 08:25 AM

    Automatic cleanup of spam.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:48 PM

    • 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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:48 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages