How can the associations like magic function when just the models are changed?

Basically desire a "has__and___goes___to__many" relationship, what must i title the table (so Rails may use it) that consists of the 2 foreign secrets?

This is guaranteed as you're following "Convention over Configuration".

Should you condition that the customer model has numerous orders then rails needs there to become a customer_id area around the orders table.

For those who have adopted these conventions then rails uses them and can have the ability to build the required SQL to locate all of the orders for any given customer.

Should you consider the development.log file when you're working on your application you'll have the ability to begin to see the necessary SQL being created to choose all orders for any given customer.

Rails doesn't create tables without you asking it to. The development of tables is accomplished by producing a migration developingOrmodify tables for you personally. Because you produce a customer model after which condition there it has_many :orders won't create an orders table. You will have to do this on your own inside a migration to produce an orders table. Within that migration you will have to either give a customer_id column or make use of the goes_to: customer statement to obtain the customer_id area put into the orders table.

Short answer: You cannot just tell the models that they are related there need to be posts within the database for this too.

Whenever you setup related models, Rails assumes you've adopted a convention which enables it to obtain the stuff you authored. This is what happens:

  1. You place in the tables.

    Following conventions in Rails, you title the table inside a particular, foreseeable way (a plural noun, e.g. people). Within this table, if you have rapport to a different table, you need to create that column and title it in another foreseeable way (e.g. bank_account_id, if you are relevant towards the bank_accounts table).

  2. You are writing one class getting from ActiveRecord::Base

    class Person < ActiveRecord::Base

    Whenever you instantiate one of these simple models, the ActiveRecord::Base constructor compares the title from the class, converts it to lowercase and pluralizes it. Here, by reading through Person, it yields people, the title on the table we produced earlier. Now ActiveRecord knows where you'll get all the details in regards to a person, also it can browse the SQL output to determine exactly what the posts are.

  3. You add associations towards the model: has_many, belongs_to or has_one.

    Whenever you type something similar to, has_many :bank_accounts, it assumes a couple of things:

    1. The title from the model that you simply connect with is BankAccount (from camel-casing :bank_accounts).

    2. The title from the column within the people table which describes a banking account is bank_account_id (from singularizing :bank_accounts).

    3. Because the relationship is has_many, ActiveRecord knows to provide you with techniques like john.bank_accounts, using plural names for things.

Putting all that together, ActiveRecord understands how to make SQL queries that provides you with an individual's accounts. It knows how to locate everything, since you adopted a naming convention it knows whenever you produced the table and it is colums.

Among the neat reasons for Ruby is you can run techniques on the whole class, and individuals techniques can also add other techniques to some class. That is what has_many and buddies do.

The rails guide with this is fairly helpful