Gerard,
Now I understand that to achieve further transparency in the way that those
two tables (ARs) cooperate I have to operate on the table with has_one macro
(so I do not have to save asociated object in before_save callback... etc).
Perhaps I will do that, but it will be very complicated since I incorporated
login engine to the app and I will have to reimplement it to work not on
User AR but on other AR that has_one user :-)
Currently I made my solution generic to the bone... Thanks to Ruby
flexibility I wrote methods that add artificial accessors and call backs
dynamically... I sleep better now... :]
Thanks a bunch,
Szczepan
2006/1/4, Gerard <mailing@gp-net.nl>:>
> Szczepan
>
> You further down the Rails then I am, but the agile book states the
> following:
>
> You can associate an invoice with an order from either side of the
> relationship. You can tell an order that it has an invoice associated with
> it, or you can tell the invoice that it''s associated with an
order. The
> two
> are almost equivalent. The difference is in the way they save (or
don''t
> save)
> objects to the database. If you assign an object to a has_one association
> in
> an existing object, that associated object will be automattically saved.
>
> If instead you assign a new object to a belongs_to association, it will
> never
> be automatically saved.
>
> Looking at what you explained below I don''t see another solution.
A
> probably
> unusable option would be to have table_b has_one table_a, because then a
> field table_a_id in table_b would do the trick. But it seems this could
> get
> in the way of you (already in place) application logic working table_a.
> (But
> you would be the one to examine that).
>
> I know where your coming from though. If something isn''t to my
liking I
> can go
> nuts on it. But on a brighter note .. If it works.
>
> Regards,
>
> Gerard.
>
>
>
> On Wednesday 04 January 2006 00:55, Szczepan Faber tried to type something
> like:
> > Gerard,
> >
> > In fact, I am using "has_one" and "belongs_to"
combination but this
> > solution hase some drawbacks.
> >
> > I am working on new version of one application that has very rigid
> schema
> > that I don''t want to touch.
> > To extend specific table (table_a) I created other table (table_b)
that
> has
> > some additional data and is associated one-to-one.
> >
> > table_a has_one table_b
> > table_b belongs_to table_a
> >
> > Then, I can access table_a from table_b: table_b.table_a
> >
> > Currently I have to do with table_b
> > 1. Add field accessors that will delegate setters / getters to
> > table_b.table_a, e.g.
> >
> > def user
> > self.table_a ||= Table_a.new
> > self.table_a.user
> > end
> >
> > 2. Write before_save callback that will save the table_b.table_a so
> the
> > foreign key constraint will pass and assign foreign key value:
> >
> > def before_save
> > unless self.table_a.save
> > raise("xxx")
> > end
> > self.table_a_id = table_a.id
> > end
> >
> > 4. Write additional validate method that will delegate validation to
> > table_b.table_a
> > 5. Add initialization of table_a in table_b constructor
> >
> > I hate this solution, though...
> >
> > All this affort is to make it possible to edit all data from both
tables
> in
> > one view.
> >
> > The question is if I can do it somehow more ellegant.
> >
> > Really apreciated your help Gerard,
> >
> > Regards,
> > Szczepan
> >
> > 2006/1/3, Gerard <mailing@gp-net.nl>:
> > > Szczepan,
> > >
> > > Still not all the way clear. However, AR understands
"has_one" and
> > > "belongs_to". Did you mean something like this:
> > >
> > > class Order < ActiveRecord::Base
> > > has_one :invoice
> > > end
> > >
> > > class Invoice < ActiveRecord::Base
> > > belongs_to :order
> > > end
> > >
> > > --snip--
> > > invoice = Invoice.new
> > > if invoice.save
> > > order.invoice = invoice
> > > else
> > > fail invoice.errors.to_s
> > > end
> > >
> > > Regards,
> > >
> > > Gerard.
> > >
> > >
> > >
> > > On Tuesday 03 January 2006 21:56, Szczepan Faber tried to type
> something
> > >
> > > like:
> > > > I have two tables in one-to-one relationship - can I build
one
> > >
> > > ActiveRecord
> > >
> > > > ORM on both of them?
> > > >
> > > > Szczepan
> > > >
> > > > 2006/1/3, Gerard <mailing@gp-net.nl>:
> > > > > What do you mean by ''map''?
> > > > >
> > > > > Regards,
> > > > >
> > > > > Gerard.
> > > > >
> > > > > On Tuesday 03 January 2006 21:28, Szczepan Faber tried
to type
> > >
> > > something
> > >
> > > > > like:
> > > > > > Can I map 2 tables into one class?
> > > > > >
> > > > > > Is there any pattern, e.g. using ActiveRecord
aggregation?
> > > > > >
> > > > > > Thanks
> > > > > > Szczepan
> > > > >
> > > > > --
> > > > > "Who cares if it doesn''t do anything? It
was made with our new
> > > > > Triple-Iso-Bifurcated-Krypton-Gate-MOS process
..."
> > > > >
> > > > > My $Grtz =~ Gerard;
> > > > > ~
> > > > >
> > > > > :wq!
> > >
> > > --
> > > "Who cares if it doesn''t do anything? It was made
with our new
> > > Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."
> > >
> > > My $Grtz =~ Gerard;
> > > ~
> > >
> > > :wq!
>
> --
> "Who cares if it doesn''t do anything? It was made with our
new
> Triple-Iso-Bifurcated-Krypton-Gate-MOS process ..."
>
> My $Grtz =~ Gerard;
> ~
> :wq!
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060104/01b369aa/attachment-0001.html