failed to save using append operator.

0 views
Skip to first unread message

sufian...@gmail.com

unread,
Apr 21, 2008, 3:41:54 AM4/21/08
to Malaysia Ruby Brigade
Hi everyone...
I got some problem..
I got 2 related tables.. and its DDL looklike this..

DDL
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++
CREATE TABLE tblvilages (
icnumber character varying(15) PRIMARY KEY,
name character varying(80),
gender character varying(1)
);

CREATE TABLE tblvilagesaddress (
runningid serial PRIMARY KEY,
icnumber character varying(15) REFERENCES tblvilages(icnumber) ON
DELETE CASCADE,
address1 varchar(50),
address2 varchar(50),
address3 varchar(50)
);



Controller
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++

def experiment

@datas = []
@datas[0] = {"icnumber"=>"870412039981", "name"=>"MOHD
YUSRIZAL B. MOHD YUSOF", "gender"=>"L", "address1"=>"Jalan Aru",
"address2"=>"Off Jalan Sultan Sulaiman", "address3"=>"Taman Sri
Angkasa"}
@datas[1] = {"icnumber"=>"870107018824", "name"=>"ZAILAWATI
BTE MARKISAN", "gender"=>"P", "address1"=>"Pos 23, Lorong Atap",
"address2"=>"Taman Segar", "address3"=>""}
@datas[2] = {"icnumber"=>"861121091122", "name"=>"ASYANTI A/P
MURUGAN", "gender"=>"P", "address1"=>"No. 23, Jalan Utama",
"address2"=>"Mukim Segantang", "address3"=>""}
@datas[3] = {"icnumber"=>"870513076654", "name"=>"LEE KIM
SHE", "gender"=>"P", "address1"=>"No. 29, Kampung Paya Jarak",
"address2"=>"Mukim Sepanggar", "address3"=>""}
@datas[4] = {"icnumber"=>"870612091121", "name"=>"MOHD ABBAS
BIN SUHAIMI", "gender"=>"L", "address1"=>"A-9-12, Apartment Sri
Andalas,", "address2"=>"Jalan Sri Kundang", "address3"=>"Taman Segar"}

@datas.each do |data|

test_person = {}
test_person["icnumber"] = data["icnumber"]
test_person["name"] = data["name"]
test_person["gender"] = data["gender"]

@person = TestPerson.new(test_person)
@person.id = test_person["icnumber"]


test_person_address = {}
test_person_address["icnumber"] = @person.id
test_person_address["address1"] = data["address1"]
test_person_address["address2"] = data["address2"]
test_person_address["address3"] = data["address3"]

@person_address =
TestPersonAddress.new(test_person_address)

# method 1
@person.save
@person_address.save

# method 2
TestPersonAddress.transaction do
@person.save
@person_address.save
end

# method 3
TestPerson.transaction do
@person.save
@person_address.save
end

# method 4
@person.test_person_addresses << @person_address
@person.save


end

end



model : TestPerson
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++
class TestPerson < ActiveRecord::Base

set_table_name "tblvilages"
set_primary_key :icnumber

has_many :test_person_addresses

end


model : TestPersonAddress
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++
class TestPersonAddress < ActiveRecord::Base

set_table_name "tblvilagesaddress"
set_primary_key :runningid


belongs_to :test_person, :class_name=>"TestPerson", :foreign_key=>:icnumber

end



problem:

actually I've try 4 different method (as shown in the controller) to
insert the data into the database,
but, only method 1 and 2 successful.. and method 3 and 4 was failed.
Actually my goal is to use method 4. I've try this using 2 different
databases (Postgresql and MSSQL)
but the result still the same... anybody can explain to me why method
3 and 4 results failure ?


If I used method 3 and 4, the result will look like this:

PGError: ERROR: insert or update on table "tblvilagesaddress"
violates foreign key constraint "tblvilagesaddress_icnumber_fkey"
DETAIL: Key (icnumber)=(870412039981) is not present in table
"tblvilages".
: INSERT INTO tblvilagesaddress ("address1", "address2", "address3",
"icnumber") VALUES('Jalan Aru', 'Off Jalan Sultan Sulaiman', 'Taman
Sri Angkasa', '870412039981')


notes: I need to remain the DLL's naming column and model's naming for
some reason...

thanks

sufian...@gmail.com

unread,
Apr 21, 2008, 3:42:50 AM4/21/08
to Malaysia Ruby Brigade

sufian...@gmail.com

unread,
Apr 21, 2008, 3:47:21 AM4/21/08
to Malaysia Ruby Brigade
Hi everyone...
got some problem here
I got 2 related tables .. and its DDL looks like this..
but, only method 1 and 2 was successful.. and method 3 and 4 was
failed.
Actually my goals is to use method 4. I've try this using 2 different

sudirman hassan

unread,
Apr 21, 2008, 9:28:48 AM4/21/08
to malay...@googlegroups.com
The rule is, the one on the right of << operator must be persisted to
database first before it can be appended to other model.

# method 4
> @person.test_person_addresses << @person_address
> @person.save

--
-- deman

sufian...@gmail.com

unread,
Apr 21, 2008, 8:47:00 PM4/21/08
to Malaysia Ruby Brigade
ermm.... actually I've done this style before, the result was
successful, but the different is..
that time, I used pluralization naming tables, and I created all the
foreign key column using combination of <singular table name>+"_id".

@student=Student.new(params[:student])

if params[:student_course]
@student.student_courses <<
StudentCourse.new(params[:student_course])
end

@student.save

sufian...@gmail.com

unread,
Apr 21, 2008, 9:32:39 PM4/21/08
to Malaysia Ruby Brigade
I think, I got problem in table relation...
but I'm not sure what it is..

when I try to display inserted data like this..


<% @person = TestPerson.find(:all) %>
<% @person.each do |person|%>
<%=person.icnumber%> | <%=person.name%> | <%=person.gender%> <br>

<% @person_addresses = person.test_person_addresses%>
<% @person_addresses.each do |person_address| %>
>> <%=person.runningid%> | <%=person.icnumber%> | <
%=person_address.address1%> | <%=person_address.address2%> | <
%=person_address.address3%>
<% end %>
<% end %>


this error occur...


PGError: ERROR: column tblvilagesaddress.test_person_id does not
exist
LINE 1: SELECT * FROM tblvilagesaddress WHERE
(tblvilagesaddress.tes...
^
: SELECT * FROM tblvilagesaddress WHERE
(tblvilagesaddress.test_person_id = '870412039981')

sufian...@gmail.com

unread,
Apr 21, 2008, 9:55:55 PM4/21/08
to Malaysia Ruby Brigade
I wonder why its still searching for test_person_id? I've already
declared primary key for each model and mention the actual table
name...

sufian...@gmail.com

unread,
Apr 21, 2008, 11:38:37 PM4/21/08
to Malaysia Ruby Brigade
for error in views... I've the answer..
the error ===> PGError: ERROR: column
tblvilagesaddress.test_person_id does not exist
because I just put one foreign_key in

model : TestPersonAddress
belongs_to :test_person, :class_name=>"TestPerson", :foreign_key=>:icnumber


after I do some changes in model TestPerson

from this===> has_many :test_person_addresses
to this ==>
has_many :test_person_addresses, :class_name=>"TestPersonAddress", :foreign_key=>:icnumber

the view problem solved


On Apr 22, 9:32 am, "sufian.yus...@gmail.com"

sufian...@gmail.com

unread,
Apr 28, 2008, 4:27:53 AM4/28/08
to Malaysia Ruby Brigade
found solution already...

actually I didn't mention that I used two different in this project.
after I tried with single db, I found that I don't have any problem
using append (<<)
below is the models that I used when the error occurred.

model : TestPerson
=====================
class TestPerson < ActiveRecord::Base

establish_connection(
:adapter => "postgresql",
:database => "mydb",
:username => "user",
:password => "password"
)

set_table_name "tblvilages"
set_primary_key :icnumber

has_many :test_person_addresses

end



model : TestPersonAddress
=====================
class TestPersonAddress < ActiveRecord::Base

establish_connection(
:adapter => "postgresql",
:database => "mydb",
:username => "user",
:password => "password"
)

set_table_name "tblvilagesaddress"
set_primary_key :runningid

belongs_to :test_person, :class_name=>"TestPerson", :foreign_key=>:icnumber

end



The solution is ... I didn't inherit class directly from
ActionRecord::Base for this two model and create connection for each
model
but I used to inherit the connection from additional model.Below is
the correct models.


database.yml
==========

development:
adapter: postgresql
database: college
username: user
password: password
host: 127.0.0.1

test:
adapter: postgresql
database: college
username: user
password: password
host: 127.0.0.1

production:
adapter: postgresql
database: college
username: user
password: password
host: 127.0.0.1

MYDB:
adapter: postgresql
database: Mydb
username: user
password: password
host: 127.0.0.1




model : Mydb
=====================
class Mydb < ActiveRecord::Base
establish_connection :MYDB
end



model : TestPerson
=====================
class TestPerson < Mydb

set_table_name "tblvilages"
set_primary_key :icnumber
has_many :test_person_addresses

end



model : TestPersonAddress
=====================
class TestPersonAddress < Mydb
Reply all
Reply to author
Forward
0 new messages