Article View: pl.comp.lang.ruby
Article #834Re: RoR vs PHP
From: Jaroslaw Zabiello
Date: Wed, 02 Jan 2008 03:37
Date: Wed, 02 Jan 2008 03:37
51 lines
2563 bytes
2563 bytes
On Tue, 01 Jan 2008 19:45:20 -0000, Maciej Piechotka
<uzytkownik2@gmail.com> wrote:
> 3. Cały czas twierdzę, że AR nie potrafi tego zrobić z pudełka.
Aby nie było, że tylko Sequel to potrafi. Masz tu implementację przykładu
3 w Active Record. Pomijam tylko migracje tworzące tabelki.
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection({
:adapter => 'mysql',
:database => 'test',
:username => 'root',
:password => ''
})
class Parent < ActiveRecord::Base
end
class Child1 < ActiveRecord::Base
self.table_name = 'childs1'
self.primary_key = 'parent_id'
class << self
def next_id
maximum(:parent_id).to_i + 1
end
end
before_save do
begin
Parent.find(Child1.next_id)
rescue ActiveRecord::RecordNotFound
raise 'Parent cannot assign more childs1 objects'
end
end
end
Parent.create :pole_parent => 'Alojzy'
Parent.find :all
# [#<Parent id: 6, pole_parent: "Alojzy">]
Child1.create :pole1 => 'Jas'
Child1.find :all
[#<Child1 parent_id: 1, pole1: "Jas">]
Child1.create :pole1 => 'Malgosia'
# => Parent cannot assign more childs1 objects
--
Jarosław Zabiełło
http://blog.zabiello.com
Technical Details
Message-ID:
<op.t399og15r2rg33@jzmbp.local>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mailReferences:
<fl6ahk$m1q$1@inews.gazeta.pl> <op.t351icnsr2rg33@jzmbp.local> <fl93bh$d8p$1@inews.gazeta.pl> <r4il45-1ar.ln1@router.piechotka.com.pl> <op.t358bkfwr2rg33@jzmbp.local> <fl9aau$9b9$5@inews.gazeta.pl> <20071231084648.6d47feda.gryf_esm-no@spam-o2.pl> <op.t37dq3ydr2rg33@jzmbp.local> <0enn45-u25.ln1@router.piechotka.com.pl> <op.t37qnpdrr2rg33@jzmbp.local> <3pbo45-ha7.ln1@router.piechotka.com.pl> <op.t3777x1jr2rg33@jzmbp.local> <jtip45-n8c.ln1@router.piechotka.com.pl> <op.t388lvhur2rg33@jzmbp.local> <h96q45-37f.ln1@router.piechotka.com.pl> <op.t39jhmarr2rg33@jzmbp.local> <grhq45-5dg.ln1@router.piechotka.com.pl>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mail From: "Jaroslaw Zabiello" <abuse@tpnet.pl> Newsgroups: pl.comp.lang.ruby Subject: Re: RoR vs PHP Date: Wed, 02 Jan 2008 03:37:18 -0000 Organization: "Portal Gazeta.pl -> http://www.gazeta.pl" Lines: 51 Message-ID: <op.t399og15r2rg33@jzmbp.local> References: <fl6ahk$m1q$1@inews.gazeta.pl> <op.t351icnsr2rg33@jzmbp.local> <fl93bh$d8p$1@inews.gazeta.pl> <r4il45-1ar.ln1@router.piechotka.com.pl> <op.t358bkfwr2rg33@jzmbp.local> <fl9aau$9b9$5@inews.gazeta.pl> <20071231084648.6d47feda.gryf_esm-no@spam-o2.pl> <op.t37dq3ydr2rg33@jzmbp.local> <0enn45-u25.ln1@router.piechotka.com.pl> <op.t37qnpdrr2rg33@jzmbp.local> <3pbo45-ha7.ln1@router.piechotka.com.pl> <op.t3777x1jr2rg33@jzmbp.local> <jtip45-n8c.ln1@router.piechotka.com.pl> <op.t388lvhur2rg33@jzmbp.local> <h96q45-37f.ln1@router.piechotka.com.pl> <op.t39jhmarr2rg33@jzmbp.local> <grhq45-5dg.ln1@router.piechotka.com.pl> NNTP-Posting-Host: 89.124.240.242 Mime-Version: 1.0 X-Trace: inews.gazeta.pl 1199245040 18981 89.124.240.242 (2 Jan 2008 03:37:20 GMT) X-Complaints-To: usenet@agora.pl NNTP-Posting-Date: Wed, 2 Jan 2008 03:37:20 +0000 (UTC) X-User: hipertracker User-Agent: Opera Mail/9.25 (MacIntel) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit