let's say there are 2 models.
user model:
has_many :posts
post model:
belongs_to :user
has_many_attached :files, dependent: :destroy
what I want is simply all files of the user. something like:
has _may :post_files , through: posts, class_name: "XXX"
or any other way which can give me all the files of the user.
so I want all files of all posts which belong to the user. like user.post_files
thank you all for your answers. I found the solution.
has_many_attached :files actually sets two has_many relationships:
has_many :files_attachments and
has_many :files_blobs
So in user.rb (parent model) we can simply have:
has_many :files_attachments, through: :posts
and in this way, you can have user.files_attachments to get all files of posts for one user.
Here is how the structure should be
user.rb <-- User model
has_many :posts
post.rb <-- Post model
belongs_to :user
has_many_attached :files
this way you can do
Post.post_files
or
Post.with_attached_files.find(params[:id])
In conclusion.
The attachments belong to the Post, not to the User, so there is no need to make any call to User model
Your post model acts as an xref table between users and attached files right?
You have defined a user as having many posts and a post as having many attached files. Using has_many through: will allow a user to see all the attached files for a post
user model:
Class User < ...
has_many :posts
has_many attached_files, through: :posts
post model:
Class Post < ...
belongs_to :user
has_many_attached :files, dependent: :destroy
Class AttchedFiles < ...
belongs_to :post
This enables you to do things like
User.first.attached_files
Which will return all the attached files for the user returned by the User.first declaration
Which I believe is what you were looking for
Related
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
Tell me how to build a relationship between the models as follows:
- You can have a lot of posts
- Other users can write to each other positions on the wall (as in social networks, namely, when you can itself create a record or you can create it on another user page.
You should at least try to do it yourself, but here is solution:
User model:
User (id, name)
has_many :posts
has_many :comments
has_many :commented_posts, through: :comments
Post model:
Post (id, content, user_id)
belongs_to :user
has_many :comments
Comment model:
Comment (id, content, post_id, user_id)
belongs_to :user
belongs_to :post
If you refereing to posts as medium with which user can tag other users, so that the post can appear on their walls.
You can definetly use posts to write to other people walls. Thats how any social platform works, same post can act as standalone post like a blog user is publishing for his followers or in particular community.
I a social feed like platform, tags the users on the post is the only way to push the post on wall feed of any user.
So Here we can have the following entities.
User
class User < ActiveRecord::Base
has_many :usertags
has_many :posts
end
Post
class Post < ApplicationRecord
has_many :usertags, as: :usertagable
belongs_to :user
has_many :comments ,:as => :commentable
end
Usertag
class Usertag < ApplicationRecord
belongs_to :user
belongs_to :usertagable, :polymorphic => true
end
i have build polymorphic relations for usertags as you may extend the current schema to involves comments on the posts as well like the following comment model, which can be served using polymorphic relations.
class Comment < ApplicationRecord
# all the relations for the comment
belongs_to :user
belongs_to :post
belongs_to :commentable, :polymorphic => true
has_many :comments, :as => :commentable
has_many :usertags, as: :usertagable
end
Comment in turn belong to a users/author, post to which comment is attached, comment can also be commented so it can also belong to commentable. Also comment can have user mentioned as wells like posts can.
A social feed like platform, tags the users on the post is the only way to push the post on wall feed of any user.
Now you can easily fetch all the posts belonging to particular user along with usertagged, comments, auther of comments.
post_list = Post.eager_load(:followers, :user, :communities, :usertags => :user, :comments => [:usertags => :user]).select("*").where("user.id is ?", :user_id)
Hope this helps
Thanks.
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
Hello i have trouble with logic. In my app users can create Posts and add them to favourites. The problem is in assiciations on Posts and Users. When User creates Post user_id is applied to posts table. How can i make associations when other user or this one add Post to favourite.
You need to create another table that will join a post and user. You can call that table favorites with 2 columns: post_id and user_id
class Favorite < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
class User < ActiveRecord::Base
has_many :posts
has_many :favorites
has_many :favorite_posts, through: :favorites, source: :post
end
class Post < ActiveRecord::Base
belongs_to :user
has_many :favorites
has_many :favorited_by_users, through: :favorites, source: :user
end
You could create an new model/table for association. I would take a many to many relation for this.
Table: Bookmark
user_id | post_id
How a has many :through relationship in rails work is discriped here:
http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association
I'm trying to figure out how to best way to create associations for the following models:
User
Category
Post
Comments
Here are the requirements I'm trying to meet:
A user can have many posts
A post belongs to a user
A post can have many comments
A comment belongs to a post
A post can belong to a category
A user does NOT have many categories.
(The number of categories is fixed and the same for all users)
A category can have many posts
In terms of routing, I'd like to be able to access a post within a certain category for a specific user. For example:
http://domain.com/users/1/categories/1/posts
Since there is no direct relationship between User and Category, I'm not quite sure how to best set up the associations. And I'm totally lost on how to configure the routes.
Here's what I have for my models:
class User < ActiveRecord::Base
has_many :posts
end
class Category < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :metric
has_many :comments
end
class Comments < ActiveRecord::Base
belongs_to :post
end
Is this a case where I should be using has_many :through relationships? Or something more complex like polymorphic associations? Thanks in advance!
Yes, it would be a very good idea to use :
User has_many :comments, :through => :posts
If you like, you can also get categories comments, by :
Category has_many :comments, :through => :posts
Remember that a through association is just a facility that allows you to do things like user.comments directly (and through is the way for the association to find the user comment that is referred to post model).