Ruby on Rails and database associations -


I am new to the world of Ruby, and there is nothing clear about defining associations between models. The question is: Association has been saved?

For example, if I execute the client model:

  Model Customer Name: String Era: Integer  

And then I make an order model

  Model Order Description: Text Quantity: Integer  

and then set Affiliation in the following way:

  class clients & lt; ActiveRecord :: Base has_many: command end class command & lt; ActiveRecord :: Base is_to: Customer End  

I think there is something missing here, for example associations created with foreign key keywords "Head_may" and "Attito" among the two entities How does it manage?

Thanks

You can do this whenever you make your order

The rail conference is that each row will have a primary key called "ID", and the foreign key is the table that is referenced, followed by an underscore, then ID. Therefore in this case there will be a feature called "Customer_ID" in the order table

Since you have already generated, you should create a new migration script which will be an integer column called "customer_id" on your order table. Connects

You want to modify the no existing migration. Just create a new one that adds columns.

  class add_customer_id_to_orders & lt; ActiveRecord :: Migration DF self.up add_column: order,: customer_id,: integer end df self.down removal_column: order, customer_id end  

Note: if you have done something already To create data in your database, this will result in some empty foreign keys, which you want to fix, or just delete and create new data.


Comments