Hi all.
I have has_and_belongs_to_many association between models: Section and
Content.
class Section < ActiveRecord::Base
acts_as_tree
has_and_belongs_to_many :contents
belongs_to :default_content, :class_name => ''Content'',
:foreign_key =>
''default_content_id'', :dependent => :nullify
end
class Content < ActiveRecord::Base
belongs_to :section
end
When I trying to remove some Content which associated with Section I
have an error related to foreigh key violation:
"PGError: ERROR: update or delete on "contents" violates foreign
key
constraint "fk_contents_contents_sections" on
"contents_sections"
DETAIL: Key (id)=(7) is still referenced from table
"contents_sections".
: DELETE FROM contents
WHERE id = 7"
And when I trying to remove Content which belongs to "default_content"
of some Section
I have an error:
PGError: ERROR: update or delete on "contents" violates foreign key
constraint "fk_sections_contents" on "sections"
DETAIL: Key (id)=(8) is still referenced from table "sections".
: DELETE FROM contents
WHERE id = 8
I simply don''t understand why rails can''t determine that
relation exists and can''t
remove records from join-table transparently...
Please tell me what I must define to got happiness?
I think I should create another model like SectionContent and make
relation like ''has_many :contents, :through =>
:section_contents''... Is there another way?
Thanks in advance.
CREATE TABLE sections (
id serial NOT NULL,
parent_id integer,
default_content_id integer
);
CREATE TABLE contents (
id serial NOT NULL,
alias character varying(32) NOT NULL,
name character varying(64) NOT NULL,
title character varying(64) NOT NULL,
keywords character varying(256),
description character varying(512),
contents character varying(2048),
last_modified timestamp without time zone
);
CREATE TABLE contents_sections (
section_id integer NOT NULL,
content_id integer NOT NULL
);
ALTER TABLE ONLY contents_sections
ADD CONSTRAINT fk_contents_contents_sections FOREIGN KEY
(content_id) REFERENCES contents(id);
ALTER TABLE ONLY contents_sections
ADD CONSTRAINT fk_sections_contents_sections FOREIGN KEY
(section_id) REFERENCES sections(id);
ALTER TABLE ONLY sections
ADD CONSTRAINT fk_sections_contents FOREIGN KEY (default_content_id)
REFERENCES contents(id);