Rails 5: How can I see a model with via 2 models? - ruby-on-rails

When I do #post = Post.first.comments.build(content: "bla", user: #user) and search via #user.comments nothing shows up. But when I do it with #post.comments, it lists the comment. How can I see my comment with both #user.comments and #post.comments?
class Post < ApplicationRecord
belongs_to :user
has_many :comments
end
my user model
class User < ApplicationRecord
has_many :posts
has_many :comments
end
comment model
class Comment < ApplicationRecord
belongs_to :user
belongs_to :post
end

use through to get comments of all posts associated with the user's posts.
class User
has_many :posts
has_many :comments, through: :posts
end

Related

Setting up Rails model

I have a User model, Post model and Bookmark model. How do i need to set up relationship among them so that I can use current_user.bookmarks.posts.
Maybe:
class User < ApplicationRecord
has_many :bookmarks
end
class Bookmark < ApplicationRecord
belongs_to :user
has_many :posts
end
class Post < ApplicationRecord
belongs_to :bookmark
end
If you're wanting to get all the posts that belong to the user, then you can use the has_many :through association:
class User < ApplicationRecord
has_many :bookmarks
has_many :posts, through: :bookmarks
end
class Bookmark < ApplicationRecord
belongs_to :user
has_many :posts
end
class Post < ApplicationRecord
belongs_to :bookmark
end
Then you can just call:
user = User.first
all_posts = user.posts
It will return an array containing all the posts for each of the bookmarks belonging to the user.

Rails - touching parent of a parent

Three models: Post, Comment, Rating.
Post
class Post < ApplicationRecord
has_many :comments
has_many :ratings, through: :comments
end
Comment
class Comment < ApplicationRecord
belongs_to :post
has_one :rating
end
Rating
class Rating < ApplicationRecord
belongs_to :comment
end
I want to be able to do call a method on Post model (to for example calculate mean_rating) when the Rating record gets added or removed to my database.
I am able to to this with calling the post model method inside rating model after_create and after_destroy but I am wondering if there is a way to update Post model using dependent or any other methods?
Any ideas?
You could create a method in your Post model:
class Post < ApplicationRecord
has_many :comments
has_many :ratings, through: :comments
def average_rating
ratings.average(:note)
end
end
And call it on your instances:
post = Post.last
post.average_rating

Rails has_many_belongs_to, has_many :through

I have 3 Models: Place, User, Reviews. Place and Users related with has_many_belongs_to - for favorites, and has_many :through - for reviews.
I want to insert to favorites some place, but this place inserted to reviews table, what is wrong?
Or create model Favorites?
class Place < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :reviews
has_many :users, through: :reviews
end
class User < ActiveRecord::Base
has_and_belongs_to_many :places
has_many :reviews
has_many :places, through: :reviews
end
class Review < ActiveRecord::Base
belongs_to :user
belongs_to :place
end
class FavoritesController < ApplicationController
def add_favorites
if User.first.places.push(Place.find(params[:place_id]))
render json: {desc:true, status:0, error:nil}
else
render json: {desc:false, status:1, error: "Problem insert to table places_users"}
end
end
Have you try having different names for the associations? Right now both your favorites and reviewed places are called the same.
Try something like:
class User < ActiveRecord::Base
has_and_belongs_to_many :favorite_places
has_many :reviews
has_many :reviewed_places, through: :reviews
end
And for the controller:
User.first.favorite_places.push(Place.find(params[:place_id]))
You could always use active record serializers to store a array of favorites ids in the User model.
class User < ActiveRecord::Base
serialize :favorites
end
user = User.create(favorites: [1, 4, 8, 9])

Rails association with same model

I have a model Article and model User. In users, there will be one creator of the article and many readers. How do I link these together?
I was thinking:
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
has_one created_by, through: :user (????)
end
class User < ActiveRecord::Base
has_many :articles
end
You can do like this
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
has_one created_by_user,:class_name => 'User'
end
If you want to specify a custom foreign_key(which is useful in these cases),you can specify a foreign_key option
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
has_one created_by_user,:class_name => 'User',:foreign_key =>'your_custom_fkey'
end

has_many inheritance

I have a model called company that has_many users then users belongs_to company.
class Company < ActiveRecord::Base
has_many :users
end
class User < ActiveRecord::Base
belongs_to :company
end
If something belongs to users will it also belong to company?
You have to use has_many :through association for this.
class Comment < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
belongs_to :company
has_many :comments
end
class Company < ActiveRecord::Base
has_many :users
has_many :comments, :through => :users
end
Now you can do the following:
c = Company.first
c.users # returns users
c.comments # returns all the comments made by all the users in the company

Resources