Answers:
-
Because data sets can be related through the use of foreign keys
-
Structrured Query Language, it is a stnadard language to query database to retrieve / store data
-
Schema aka Strucre view which shows the list of the column names, data view whcih shows the actual data (rows of data)
-
The Primary key, which is usually the id
-
The forgeing key is used in rows in a table to join other rows from another table, where the foreign key is the primary key
-
About ActiveRecord pattern we can say that each table is represented by a Class and each row (of that table) by an instance of that Class. The Class usually also provides attr accessor methods to retrieve / store data in each column
-
crazy_monkeys - we can also use the very useful 'CrazyMonkey'.tableize method which returns => "crazy_monkeys"
-
Assuming the following: Project has_many issues and Issue belongs_to project than the foreign_key will be on the issues table in the db. As a simple rule the foreign_key is usually on the belongs_to model side.
-
.
class Animal < ActiveRecord::Base belongs_to :zoo end
. Schema of the table animals: . id (integer, created by default) . name (string, manually added with the mgration file) . zoo_id (integer, manually added with the migration) . Some of the methods available to the Zoo class are: . zoo = Zoo.first . zoo.animals (returns an array like structure, of all animals objects belonging to zoo) . zoo.animals << (shovel operator appends an oobject to the array) . zoo.animals.delete(animal id) (same as destroy) . zoo.animals.ids (returns array of ids of all animals assiciated to zoo) . zoo.animals.clear (set the foreign key of animals associated to zoo to NULL) . zoo.animals.find(animal id) . zoo.animals.build (instantiates a new animal but does not save to db, create does the same but saves to db) . as follows: . zoo = Zoo.find_by(name: "San Diego Zoo") assuming there is a column called :name which holds the name of the zoo . zoo.animals.create(name: "jumpster") -
Mass assignment is a way to create a instance of a ActiveRecord class by assigning all its attr_accessor in with one single line of code as in Animal.new(name: "zebra", foreign_key: "1") . To assign without mass assignment Animal.first.name=("zebra")
-
returns the first animal object from all animal instances of class Animal
-
Animal.create(name: "Joe")
-
a M:M association main aspect at a db level is that it needs a join table that has foreign keys of both sides
-
Has Many and Belongs to Many vs Has Many Through. The main difference is that HMT allows to reference the join table from rails becuase it has a Model for the join table, whereas HMBTM has the join table in the db but does not allow to reference it directly in rails
-
Suppose we have a User model and a Group model, and we have a M:M association all set up. How do we associate the two? At a Model level we would need to define the following 3 Models:
class User < ActiveRecord::Base
has_many :user_groups
has_many :groups, through: :user_groups
end
class Group < ActiveRecord::Base
has_many :user_groups
has_many :users, through: :user_groups
end
class UserGroup < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
We would finally need 3 different tables at a db level:
users
groups
user_groups (this is the join table, it has 2 foreign keys that refer to the users table and the groups table)