# Record saving problem

**URL:** <https://www.ruby-forum.com/t/record-saving-problem/174404>\
**Category:** Rails\
**Created:** [August 26, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/record-saving-problem/174404 "2009-08-26T14:15:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [August 26, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/record-saving-problem/174404/1 "2009-08-26T14:15:09Z")

</div>

Greetings all,

I have some active\_record models which have same columns, for  
instance:

model A (int id, vchar name, vchar code)  
model B (int id, vchar name, vchar code)

I can initiate a new model B object using “b = B.new”, assuming that I  
already have an “a” acquired by A.find(), how can I assign the column  
values of “a” to object “b” easily like:

```auto
b = B.new(a)
b.save
```

Cheers,  
Difei

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [August 26, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/record-saving-problem/174404/2 "2009-08-26T18:13:47Z")

</div>

On Wed, Aug 26, 2009 at 7:15 AM, Difei  
Zhao[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [code]  
> b = B.new(a)

attributes = a.attributes  
attributes.id = nil  
b = B.new( attributes )

> b.save  
> [/code]

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [August 26, 2009, 6:24pm UTC](https://www.ruby-forum.com/t/record-saving-problem/174404/3 "2009-08-26T18:24:56Z")

</div>

On Aug 26, 2009, at 11:11 AM, Greg D. wrote:

> > already have an “a” acquired by A.find(), how can I assign the column  
> > [/code]  
> > –  
> > Greg D.  
> > [http://destiney.com/](http://destiney.com/)

Actually, there’s no need to nil the id because you can’t mass-assign  
the id.

b = B.new(a.attributes)

would have exactly the same effect.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [August 27, 2009, 1:33pm UTC](https://www.ruby-forum.com/t/record-saving-problem/174404/4 "2009-08-27T13:33:46Z")

</div>

Greg D. wrote:

> On Wed, Aug 26, 2009 at 11:21 AM, Rob  
> Biedenharn[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Actually, there’s no need to nil the id because you can’t mass-assign  
> > the id.
> > 
> > b = B.new(a.attributes)
> > 
> > would have exactly the same effect.
> 
> I have 47 models with matching history models that will disagree. If  
> I don’t nil the id it complains.
> 
> –  
> Greg D.

Well, thanks very much for both of you guys.

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [August 26, 2009, 9:11pm UTC](https://www.ruby-forum.com/t/record-saving-problem/174404/5 "2009-08-26T21:11:51Z")

</div>

On Wed, Aug 26, 2009 at 11:21 AM, Rob  
Biedenharn[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Actually, there’s no need to nil the id because you can’t mass-assign  
> the id.
> 
> b = B.new(a.attributes)
> 
> would have exactly the same effect.

I have 47 models with matching history models that will disagree. If  
I don’t nil the id it complains.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
