hi,
I''ve got the following tables in mysql notation:
CREATE TABLE `nodes` (
`id` int(11) NOT NULL auto_increment,
`parent_id` int(11) default ''0'',
`group_id` int(11) default ''0'',
PRIMARY KEY (`id`),
);
CREATE TABLE `groups` (
`parent_id` int(11) NOT NULL default ''0'',
`id` int(11) NOT NULL auto_increment,
PRIMARY KEY (`parent_id`,`id`)
);
The tables have other attributes as well, but for this
question they are of little importance.
The nodes table is actually a representation of a tree
and the groups are there so that several nodes can be
grouped together. the reason why parent_id ends up in
the groups table is because the data is only allowed to
group children of the same node,
class Node < ActiveRecord::Base
belongs_to :parent,
:class_name => ''Node'',
:foreign_key => ''parent_id''
has_many :children,
:class_name => ''Node'',
:foreign_key => ''parent_id''
belongs_to :group,
:foreign_key => ?what?
end
class Group < ActiveRecord::Base
has_many :nodes # Here is also a problem...
end
I asked on irc, and got the tips that it might be possible
to do by overriding primary_key for the Group class, but
browsing around the code it seems a bit problematic to return
to values, since for instance passing two values to .find()
has completely different semantics.
Since my lack of experience with activerecord I don''t really
know where to start, and therefore I now ask what would be
the best way to acheive this, and where should I look for
advice?
!g
Hi Gustav, On 7.1.2005, at 12:59, Gustav Munkby wrote:> > CREATE TABLE `groups` ( > `parent_id` int(11) NOT NULL default ''0'', > `id` int(11) NOT NULL auto_increment, > PRIMARY KEY (`parent_id`,`id`) > );Just one note: If your id is auto_increment, it will be unique anyway. Why do you need to have a two-column primary key? Actually the table is not in third normal form at the moment. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Finally found the answer...
After diggin the API, I realise that render_action actually load the
template with the action name, but does not execute the method. so
what I should have done is just call the ''list'' method one
more time:
def add_item
@item = Todo.new
@item.attributes = @params["new_item"]
if @item.save
redirect_to(:action => "list")
else
list # <- call list method to load the @done and
@not_done variables
render_action "list"
end
end
Is this the correct way? Rails is fun:)
On Fri, 7 Jan 2005 23:25:36 +0800, Boon Hoo Thee <theebh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Finally found the answer... > > After diggin the API, I realise that render_action actually load the > template with the action name, but does not execute the method. so > what I should have done is just call the ''list'' method one more time: > > def add_item > @item = Todo.new > @item.attributes = @params["new_item"] > if @item.save > redirect_to(:action => "list") > else > list # <- call list method to load the @done and > @not_done variables > render_action "list" > end > end > > Is this the correct way? Rails is fun:)I believe the correct method would be to use a redirect so that it lets rails handle calling the new controller method. But your way should work as well.