Ruby On Rails Classroom image

Anil  Bist / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4|
Lessons:- A Problem with the Data Model (and a Solution)

To get started with the implementation, we first generate a Relationship model as follows:

$ rails generate model Relationship follower id:integer followed id:integer

 

Since we will be finding relationships by follower_id and by followed_id, we should add an index on each column for efficiency, as shown in Listing 11.1.

 

 

Listing 11.1 also includes a composite index that enforces uniqueness of pairs of (follower_id, followed_id), so that a user can’t follow another user more than once:

add index  :relationships, [:follower id, :followed id], unique:  true

 

  (Compare to the email uniqueness index from Listing 6.22.) As we’ll see starting in Section 11.1.4, our user interface won’t allow this to happen, but adding a unique index arranges to raise an error if a user tries to create duplicate relationships anyway (using, e.g., a command-line tool such as curl). We could also add a uniqueness validation to the Relationship model, but because it is always an error to create duplicate relationships, the unique index is sufficient for our purposes.

    To create the relationships table, we migrate the database and prepare the test database as usual:

 

The result is the Relationship data model shown in Figure 11.8.

 

 
 
 
image
Anil  Bist

Skills    Ruby On Rails

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)