I’m at a loss. When the application saves a new model it will not update
the fields that hold the plymorphic relationship. Below are my
tables/models.
So when I do this:
@article = Article.new(params[:article])
@content_item = ContentItem.new(params[:content_item])
@content_item.content_type = @article
@content_item.save!
I get a new record in the articles table (good), and a new record in the
content_items table (good), but the content_type and content_id fields
are null. (ugh!) I can’t figure it out.
Maybe I’m making an obvious mistake, but it looks ok to me. Anyone see
an obvious mistake?
BTW - I’m on edge rails. I had 1.1 installed but was having issues
saving has_many :through relationships, so I went for the edge.
create_table :articles do |t|
t.column :body, :text, :nil => false
end
create_table :content_items do |t|
t.column :content_node_id, :integer
t.column :name, :string, :limit => 255, :nil => false
t.column :icon, :string, :limit => 255
t.column :slug, :string, :limit => 255, :nil => false
t.column :path, :string, :limit => 255
t.column :summary, :string, :limit => 255
t.column :status, :integer, :default => 1, :nil => false
t.column :publish_at, :datetime, :default => Time.now, :nil =>
false
t.column :take_down_at, :datetime
t.column :user_id, :integer
t.column :author_rating, :integer
t.column :allow_comments, :boolean, :default => true
t.column :allow_ratings, :boolean, :default => true
t.column :content_id, :integer
t.column :content_type, :string, :limit => 25
end
class Article < ActiveRecord::Base
has_one :content_item, :as => :content_type
end
class ContentItem < ActiveRecord::Base
belongs_to :content_type, :polymorphic => true
STATUS = {:deleted => 0, :pending_approval => 1 , :published => 2}
def before_save
slug = name.downcase.gsub(/[’"]/, ‘’).gsub(/\W+/, ’ ‘).squeeze(’
').strip.gsub(/ /, ‘-’)
end
end