RoR v2.3 Model associations - ruby-on-rails

I was wondering how I'm suppose to associate my User, Post, and Comment models. It is suppose to be like so: The user can comment on any post and a post belongs to a user with a Boolean for being admin. I have been scratching my had for awhile trying to figure this out but nothing has made any sense at all.
Any help would be greatly appreciated.

In the most obvious arrangement Post would belong_to :user and has_many :comments, and Comment would both belong_to :user and belong_to :post. User would has_many :posts.

You can specify the following association in the model
# app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
# app/models/user.rb
class User < ActiveRecord::Base
has_many :comments
end
# app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
end

Related

Additional relation between records in Rails 5.0

I need your small advice help.
I have trivial models: Post and User.
A user has_many :posts and a post belongs_to :user. A post has one owner.
I need somehow add an additional relation: a post can have multiple contributors. A contributor is a user too. I need to be able to write something like this: #post.contributors (shows User records) and #user.contributed_to (shows Post records).
How do I do that?
You'll need many-to-many association here, because user has_many posts and posts has_many users.
To implement it, you need to create an additional model (for example, contribution with user_id and post_id columns)
class Contribution < ApplicationRecord
belongs_to :user
belongs_to :post
end
And your Post and User classes will contain something like this:
class Post
belongs_to :user
has_many :contributions
has_many :contributors, through: :contributions, source: :user
end
class User
has_many :posts
has_many :contributions
has_many :contributed_posts, through: :contributions, source: :post
end

How to make references between two existing classes?

Rails newbie here; so I have three classes - user, article, comment - where,
class User < ActiveRecord::Base
has_many :articles
end
class Article < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :article
end
Now I'm wanting the comments to be user specific i.e. each comment will be linked to a user (just how every article is linked to a user). How do I go about doing that apart from adding has_many and belongs_to in user.rb & comment.rb? I hope I made myself clear.
Add similar relations to User and Comment. So a comment will have an user_id as well as an article_id.
class User < ActiveRecord::Base
has_many :articles
has_many :comments
end
class Article < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :article
belongs_to :user
end
You will have to write a migration after alerting your models to add an user_id field to your comments table.
rails g migration AddUserRefToComments user:references
Then run rake db:migrate. Hope that helps.

Rails models association not working?

thanks for this commmunity, it has helped me alot already. This is the first question I have to post myself, and it is somewhat specific to my project.
I have started from MHartls great rails tutorial.
There were models for Users and Microposts and I have added a Cars model, where each user can have a car (referenced by the car_id).
In the console I can create a user and a micropost, and assign the user to the micropost by stating post.user = michael and then it would set the post's user_id to michaels id.
I am trying to do the same for cars, where i can set a users car_id by stating michael.car = somecar but it gives an error even though the model associations LOOK exactly the same, and I have even remigrated the car_id to the user model as reference. See below:
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
belongs_to :cars
class Micropost < ActiveRecord::Base
belongs_to :user
belongs_to :car
class Car < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :users
accepts_nested_attributes_for :users
accepts_nested_attributes_for :microposts
Here is my last migration for the car_id:
class Addreferencecartousers < ActiveRecord::Migration
def change
add_reference :users, :car
add_foreign_key :users, :cars
end
end
in synch with what I had done for the user/micropost connection.
I am kind of lost on where else to look for why this does not work, does anyone have any hints/pointers on what I am missing here?
To address your problem, the belongs_to :cars in the User model should be belongs_to :car, but in a reality I believe it will be more sense to call it as has_many :cars rather than a belongs_to :car because a user can have many cars and a car belongs to user

Rails: collect through association?

For example, Topic has many comment, and each comment belongs to a user.
How can I get all the users have commented on the one topic, efficiently?
Now I doing this by
#commenters = #topic.comments.collect do |post|
user = post.user
user
end
And, how can I make #commenters uniq? Turn it into an array?
You could define through relation
Rails through association
Topic model
class Topic < ActiveRecord::Base
...
has_many :comments
has_many :users,
:through => :comments # add this line, it will enable association
...
end
Comment model
class Comment < ActiveRecord::Base
..
belongs_to :topic
belongs_to :user
..
end
User model
class User < ActiveRecord::Base
...
has_many :comments
...
end
then you can find users on topic.
#topic.users

Rails display attribute through indirect association

New to rails here. My associations are setup as below, it might not be the ideal way so any recommendation is greatly welcomed:
User has many posts (posts table has user_id)
User has one profile (profile table has user_id)
Within my post controller view, I want to display the name of the profile (profile.name) that belongs to the user who owns the post.
My naive first guess was <%= #post.user.profile.name %> but that obviously didn't work.
This is the association defined in my models:
class User < ActiveRecord::Base
has_many :posts
has_one :profile
end
class Post < ActiveRecord::Base
belongs_to :user
end
class Profile < ActiveRecord::Base
belongs_to :user
end
This is the code in the view: <%= #post.user.profile.name %>
The error is undefined method profile
you have to mention the following in your models
user.rb
has_many :posts
has_one :profile
profile.rb
belongs_to :user
post.rb
belongs_to :user
Then <%= #post.user.profile.name %> should work.
And if still there is a problem in your data
You need to define a belongs_to relation in posts and profile model to get this work. Eg:
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
end
Do the same with the profile model as well

Resources