The Blog App - Association
Introduction
Because we used the scaffold generator for the posts and comments in our blog application, the Active Record design pattern is “pre-wired.” This means:
- – A SQLite database able to store posts and comments was created when we ran the migrations.
- – A connection to this database was established.
- – The ORM for Post and Comment objects was set up — the “M” in MVC. However, there’s one thing missing — we need to ensure that any comments entered for a particular post are permanently linked to that post.
Associations in Rails
- We did make the connection between posts and comments in the database — recall that a post_id can be stored with each comment.
- To make our models in Rails fully functional we need to add associations — each post needs to know the list of comments associated with it, and each comment needs to know which post it belongs to.
- There’s a many-to-one relationship between comments and posts — a post has many comments, and a comment belongs to a post:
- The ActiveRecord module contains a set of class methods for tying objects together through foreign keys.
- To enable these, you must declare associations within your models using the following:
Relationship Model with no foreign key Model with foreign key one-to-one has_one belongs_to many-to-one has_many belongs_to many-to-many has_and_belongs_to_many
Latest Post
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Combine JavaScript and CSS
- Inline Small JavaScript and CSS
- Minify JavaScript and CSS
- Defer Parsing of JavaScript
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Percentile
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync