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: [code] b = B.new(a) b.save [/code] Cheers, Difei -- Posted via ruby-forum.com.
On Wed, Aug 26, 2009 at 7:15 AM, Difei Zhao<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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: > > [code] > b = B.new(a)attributes = a.attributes attributes.id = nil b = B.new( attributes )> b.save > [/code]-- Greg Donald destiney.com
On Aug 26, 2009, at 11:11 AM, Greg Donald wrote:> On Wed, Aug 26, 2009 at 7:15 AM, Difei > Zhao<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> 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: >> >> [code] >> b = B.new(a) > > attributes = a.attributes > attributes.id = nil > b = B.new( attributes ) > >> b.save >> [/code] > -- > Greg Donald > destiney.comActually, 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 Biedenharn agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
On Wed, Aug 26, 2009 at 11:21 AM, Rob Biedenharn<Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> 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 Donald destiney.com
Greg Donald wrote:> On Wed, Aug 26, 2009 at 11:21 AM, Rob > Biedenharn<Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> 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 DonaldWell, thanks very much for both of you guys. -- Posted via ruby-forum.com.