Assigning name from table 1 to table 2

Hi all,

I am new in Rials, I have problem to assign the first name from (model

  • name1) to first-name (model name2)

cuz when i create a name in table name1, i want it appears in table
name2

and my model relationship is:

model name1 - ( has_one :name2)
model name2 = (belongs_to :name)

please help.

thanks

First, I don’t really understand the logic to do that. But, OK, it’s
up to you to define it.
If you have the attribute 'first_name defined both in the two models,
you should do something like this:

model_name1 = Name1.create(:first_name=>‘toto’)
model_name2 = Name2.new
model_name2.first_name = model_name1.first_name
model_name2.save!
model_name1.model_name2 = model_name2

See more details on Ruby on Rails guides here:
http://guides.rubyonrails.org/

On 2 June 2011 19:11, joanne [email protected] wrote:

model name1 - ( has_one :name2)
model name2 = (belongs_to :name)

If you mean that you have two tables, both with a ‘name’ column and
you want the same data in both columns then I advise against this. It
is generally a bad idea to have the same data in two tables in the
database, you have to be very careful to keep them the same when the
name is edited for example. Also there is no need since you have
defined a relationship between the tables. So if you have a name1
record in @name1 and the name column is in table name2 then you can
say @name1.name2.name to get the name.

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs