Polymorhic Relationships Will Not Save

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

I think instead of

belongs_to :content_type
and
:as=> content_type

you want to change ‘content_type’ to just ‘content’.

Joe wrote:

I think instead of

belongs_to :content_type
and
:as=> content_type

you want to change ‘content_type’ to just ‘content’.

Wow. I’m a dumb ass. You had it right. By mistake I named the field
“content_type”, with the intention of referring to is as
“content_type”…when in reality I had to refer to it as
“content_type_type”. know what I mean? Anyway…I see what I did wrong
now. Thanks.

Joe wrote:

I think instead of

belongs_to :content_type
and
:as=> content_type

you want to change ‘content_type’ to just ‘content’.

That didn’t work either. However, I did make the “content_type” one
word, instead of two, and the content_id column will update with the
appopriate Article.ID, but the contenttype will not update with the
class name…that should be populated with “Article”. Pretty stange huh?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs