Benedikt Heinen
2005-Jun-14 00:35 UTC
Newbie questions (acts_as_tree && scaffolds; multi-key uniqueness)
Hello,
I''m a newbie to rails (and still somewhat of a newbie to ruby itself);
still I hope that my questions doesn''t immediately qualify as TO
stupid...
I''m trying to build a simple app (sort of my own personally glorified
event calendar). I would like to assign each event to a region that it
takes place in (e.g. central London, sw London, west London, ...), and
I''d
like to order the places in a hierarchy.
I''ve created a simple model "area"
CREATE TABLE areas (
id INT NOT NULL auto_increment,
parent_id INT NULL,
name varchar(40) NOT NULL,
description varchar(40) NULL,
PRIMARY KEY (id),
CONSTRAINT fk_area FOREIGN KEY (parent_id) REFERENCES areas(id),
CONSTRAINT fk_subarea UNIQUE INDEX ( parent_id, name )
)
The basic initial entries I generated make up the following areas:
UK
\- London
\- central
\- Earl''s Court
\- South Kensington
\- sw
\- Richmond
\- Twickenham
\- w
\- Chiswick
\- Ealing
I then generated a scaffold (./script/generate scaffold Area Admin) for
the area model and updated the Area model to be
class Area < ActiveRecord::Base
acts_as_tree :order => name
validates_presence_of :name
end
After starting the server (./script/server), looking at the list of
regions, I can''t see how the regions are linked; also, when I want to
create a new region, I can''t specify the parent_id.
Is there any (simple/existing) way to get the scaffold generator to
generate a scaffold that will address this? Or do acts_as_tree /
acts_as_list need to be handled manually?
Also, as can be seen in the DDL for the area table, there is a unique
index on parent + name (i.e. I would foresee that town names may be
duplicate (or that you might want to add
''n'',''e'',''s'',''w'',''central''
regions to several cities (without having to specify distinct names for
each city) - but each parent should only have one of these areas as an
immediate child.
If I see it correctly, ''validates_uniqueness_of :parent_id,
:name'' will
require EACH listed attribute individually to be unique. Is there a good
way to specify that a set of attributes needs to be unique?
Benedikt
--
Gaudeo te illud de me rogavisse.
Duane Johnson
2005-Jun-14 04:01 UTC
Re: Newbie questions (acts_as_tree && scaffolds; multi-key uniqueness)
> After starting the server (./script/server), looking at the list of > regions, I can''t see how the regions are linked; also, when I want > to create a new region, I can''t specify the parent_id. > > Is there any (simple/existing) way to get the scaffold generator to > generate a scaffold that will address this? Or do acts_as_tree / > acts_as_list need to be handled manually? >Yes, they need to be handled manually. The same goes for any associations that you make, such as has_many. Scaffolding does not assume to know what you mean in those cases.> Also, as can be seen in the DDL for the area table, there is a > unique index on parent + name (i.e. I would foresee that town > names may be duplicate (or that you might want to add > ''n'',''e'',''s'',''w'',''central'' regions to several cities (without having > to specify distinct names for each city) - but each parent should > only have one of these areas as an immediate child. > > If I see it correctly, ''validates_uniqueness_of :parent_id, :name'' > will require EACH listed attribute individually to be unique. Is > there a good way to specify that a set of attributes needs to be > unique?From a message by Dee Zsombar, dated May 5th:> validates_uniqueness_of :title, :scope => :author_id, :message > => "A book already exists with this title and author"Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails