# Iterating through each record of a model and copy that to an

**URL:** <https://www.ruby-forum.com/t/iterating-through-each-record-of-a-model-and-copy-that-to-an/149563>\
**Category:** Rails\
**Created:** [September 26, 2008, 8:28am UTC](https://www.ruby-forum.com/t/iterating-through-each-record-of-a-model-and-copy-that-to-an/149563 "2008-09-26T08:28:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 26, 2008, 8:28am UTC](https://www.ruby-forum.com/t/iterating-through-each-record-of-a-model-and-copy-that-to-an/149563/1 "2008-09-26T08:28:22Z")

</div>

Hi  
I have to copy all the service desk cis to incident cis as part of  
converting sd\_ticket to incident…So in the Incident model I did  
sd\_ticket=ServiceDeskTicket.find(sd\_id)  
Then a new incident @incident is created here  
sd\_cis=sd\_ticket.get\_sd\_cis #here I got all the service desk cis  
(Relation is ServiceDeskTicket has\_many ServiceDeskCis  
@incident.incident\_cis.add\_incident\_cis(sd\_cis) unless sd\_cis.size == 0  
#here what I am trying is to add all the sd\_cis to incident\_cis table

```
So in IncidentCi class

```

def self.add\_incident\_cis(cis\_array)  
cis\_array.each do |ci|  
#puts 'In Incidentci cis are : ‘+ci.ci\_id.to\_s  
self.incident\_id=self.id  
self.ci\_id=ci.ci\_id  
#then tries to save record  
end  
end  
This gives me exception And this error I expected since the usage of  
self  
Exception in Incident is undefined method `incident\_id=’ for  
IncidentCi:Class  
What I was trying to do the array iterates in that each sd ci  
information is there and get the required ones and copy that to  
corresponding incident cis and save  
But is there any other way to do this.Please help

service\_desk\_cis table has the structure

id integer | not null default  
nextval(‘service\_desk\_cis\_id\_seq’::regclass)  
service\_desk\_ticket\_id | integer  
ci\_id | integer |  
service\_desk\_ci\_association\_type\_id | integer |  
modified\_by | integer |  
created\_on | timestamp without time zone | not  
null  
created\_at | timestamp without time zone | not  
null  
updated\_on | timestamp without time zone | not  
null  
updated\_at | timestamp without time zone | not  
null

incident\_cis table has the structure

id integer | not null default  
nextval(‘incident\_cis\_id\_seq’::regclass)  
incident\_id | integer |  
ci\_id | integer |  
incident\_ci\_association\_type\_id | integer |  
modified\_by | integer |  
created\_on | timestamp without time zone | not null  
created\_at | timestamp without time zone | not null  
updated\_on | timestamp without time zone | not null  
updated\_at | timestamp without time zone | not null

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 26, 2008, 9:50am UTC](https://www.ruby-forum.com/t/iterating-through-each-record-of-a-model-and-copy-that-to-an/149563/2 "2008-09-26T09:50:48Z")

</div>

On Sep 26, 7:28 am, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> So in IncidentCi class
> 
> ```
> 
> def self.add\_incident\_cis(cis\_array)  
> cis\_array.each do |ci|  
> #puts 'In Incidentci cis are : '+ci.ci\_id.to\_s  
> self.incident\_id=self.id  
> self.ci\_id=ci.ci\_id  
> #then tries to save record  
> end  
> end

You should be creating a new instance of IncidentCi and setting values  
on that (and the values should be coming from ci).  
self shouldn’t be used in that way at all in this method - it’s just  
nonsensical.

Fred
