Tell me how to build a connection between the models as follows:
- User can have many posts
- Different users can write posts to each other on the wall (as in social networks, namely when you can create an entry yourself or another user can create it on your page.
|
1 answer
User Model:
# models/user.rb class User < ApplicationRecord has_many :posts has_one :wall end Post model:
# models/post.rb class Post < ApplicationRecord belongs_to :posts belongs_to :wall end Model wall / blog, etc.:
# models/wall.rb class Wall < ApplicationRecord has_many :posts belongs_to :user end Thus, you can create entries that will have an author and which will be tied to a specific wall. The request for wall entries will look like this:
@posts = Wall.find(wall_id).posts Request for specific user records:
@user_posts = Wall.find(wall_id).posts.where(user: @user) To get the wall of a particular user:
@wall = @user.wall To get entries from the wall of a particular user:
@posts = @user.wall.posts To get wall owner records:
@owner_posts = @user.wall.posts.where(user: @user) |