I have 3 models: Users, Articles and Comments.
Articel has many Comments.
Comments belongs to Article.
User has many comments.
Textbook case :)
I would like to make a list of Article IDs where the User has made Comments on. How do I do that?
I've tried variants of User.find(1).comments.articles_ids in the console but that gives me a undefined method error.
I'm sure it's simple, but I cant't figure it out :)
Thanks!
One way of doing it, that works with your current setup:
user.comments.collect(&:article_id).uniq
Alternatively I guess you could add a has_many :through to User:
class User < ActiveRecord::Base
has_many :articles, :through => :comments
end
then you can get the ids via user.articles.collect(&:id) (perhaps also user.article_ids, but I'm not sure about that).
Related
I have a has_many through relationship between the class Conversation and the class User through the class Membership.
I am trying to find all conversations where the user_id is equal to the #current_user.
I am struggling to find a simple answer but I answers usually point to something like this working.
Conversation.includes(:users, :memberships).where('users_id = %s' #current_user.id)
I can't seem to find another way. Any help would be appreciated
If users has_many conversations through membership:
#current_user.conversations
This should return all conversations that is associated to current user through your membership model.
Check out http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
I need help fixing this relationship in Rails 4.1,
User Model Relationship
has_and_belongs_to_many :blogs, join_table: 'blogs_users'
has_many :posts, through: :blogs
Blog Model Relationship
has_many :posts
Post Model Relationship
belongs_to :blog
belongs_to :user
Issue?
The following, in rails c, works - how ever the user id is not passed in:
User.find_by_x(x).blogs.find_by_x(x).post.create(x)
The post is created for a user who has said blog attached, but because of how this type of chaining works, only the blog_id is passed to the post and not the user_id Its crucial I have both blog and user id.
So I thought, why not just create the post through the user object and pass in the blog id.
User.find_by_x(x).posts.create(x, blog_id: y) # Where y represents a blog the user is associated with.
Well that's all good and stuff accept I get this error:
ActiveRecord::HasManyThroughNestedAssociationsAreReadonly: Cannot modify association 'User#posts' because it goes through more than one other association.
I believe the issue is the fact that I have a join table and I am saying has_many :posts, through: blogs Most people might assume that "just remove the through part." How ever I need this type of relationship where a user can have many blogs and blogs can belong to many users and posts belongs to a blog and to a user but a user can have many posts ....
So is there a way to fix this to keep my concept or ...
the way you are creating posts does not make much sense.
this would be the usual way of doing it:
blog = current_user.blogs.find(params[:blog_id])
post = blog.posts.create(params[:post])
respond_with(post)
The correct solution to this issue is a couple of things, one I needed to make sure that the realtionship between blog and user was the same as user and blog so in the blogs model I added:
has_and_belongs_to_many :users, join_table: 'blogs_users'
Then I needed to create posts at a post level, not a user level. So instead of, #user.posts.create() I need to do Post.create(blog_id: x, user_id: x) and then #user.posts
will work properly (assuming the user has 2 blogs with one post in each you'll get back both posts)
Say I have an article model, user model and comment model. If I want to get all users that commented on a particular article is this the best way or is there a better way?
User.find(Article.first.comments.pluck(:user_id))
You have a few options.
You can add
has_many :users, through: :comments
to Article then just say:
#article.users
If you don't want to do that for some reason you can do
#article.comments.collect(&:user)
I think this would be more efficient:
#article.comments.includes(:users).collect(&:user)
I hope that helps.
A Post belongs_to a User, and a User has_many Posts.
A Post also belongs_to a Topic, and a Topic has_many Posts.
class User < ActiveRecord::Base
has_many :posts
end
class Topic < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :topic
end
Well, that's pretty simple and very easy to set up, but when I display a Topic, I not only want all of the Posts for that Topic, but also the user_name and the user_photo of the User that made that Post. However, those attributes are stored in the User model and not tied to the Topic. So how would I go about setting that up?
Maybe it can already be called since the Post model has two foreign keys, one for the User and one for the Topic?
Or, maybe this is some sort of "one-way" has_many through assiociation. Like the Post would be the join model, and a Topic would has_many :users, :through => :posts. But the reverse of this is not true. Like a User does NOT has_many :topics.
So would this even need to be has_many :though association? I guess I'm just a little confused on what the controller would look like to call both the Post and the User of that Post for a give Topic.
Edit: Seriously, thank you to all that weighed in. I chose tal's answer because I used his code for my controller; however, I could have just as easily chosen either j.'s or tim's instead. Thank you both as well. This was so damn simple to implement. I think today might mark the beginning of my love affair with rails.
you can get the user_name and user_photo when displaying a topic and its posts...
something like:
#topic.posts.each do |post|
user_name = post.user.name
user_photo = post.user.photo
end
it's simple. sorry if i didn't understand your question very well.
Well, if what you want is display the user name of the author of a post, for example, you can do something like (not tested, but should work) :
#posts = topic.posts.all(:include => :user)
it should generate one request for all posts, and one for users, and the posts collection should have users.
in a view (haml here) :
- #posts.each do |post|
= post.user.name
= post.body
If I understand your question correctly, no, a has_many :through association is not required here.
If you wanted to display a list of all users that posted in a topic (skipping the post information), then it would be helpful.
You'll want to eager load the users from posts, though, to prevent n+1 queries.
Im creating my own blog managing app in rails (for experimental purposes).... What would the best way to get this done?
I have posts and categories.
I want to have a dropdown of categories for the user to select one when they create a new post.
Now, each user will have different privileges so not all categories should appear for all users....
Right now Im at the point where I can create posts and choose which category I want... I havent added any filter per user support....
please help me on where should I go now??
First you will need to implement authentication and authorization. There are many good Rails tutorials on these subjects so I won't go into more detail here.
At this point you will have models for User, Post, and Category. You need a list per-user of authorized categories. A naive model:
class User < ActiveRecord::Base
has_and_belongs_to_many :categories
end
But that is misleading, because a user isn't actually "in" any categories. You probably want something like a join model like so:
class UserCategoryAuthorization < ActiveRecord::Base
belongs_to :user
belongs_to :category
// More fields here; possibly:
// belongs_to :authorized_by, :class_name => 'User'
end
class User < ActiveRecord::Base
has_many :user_category_authorizations
has_many :authorized_categories,
:through => :user_category_authorizations,
:source => :category
end
To start with I would give Users a has_many categories relationship(Which you could turn into its own model object at some point if this idea gets more complicated..or now if it already makes sense) and then assuming you already have log in functionality you can ask the logged in user for its categories and populate the drop down appropriately.
If this is a security issue rather than just convenience then you will need to validate the chosen category is in the users categories when the form is submitted back to the server.
If you don't already have logins I believe there are several rails plug-ins that attempt to help you get this functionality quickly.