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
Related
I'm relatively new to Rails, so apologies in advance if this is an obvious question. I could not find an answer through searching.
I'm building a basic application which acts as an educational course - users can view lessons, take in the content, and mark lessons as 'complete' accordingly.
I would like users to be able to see which lessons are complete, by marking them as such in the course overview.
I have a Users model and a Lessons model, and the lessons are identical per user at present. If lessons were unique per user, this would presumably be solvable with a boolean 'complete' column for each lesson. This is not the case in this application, however - some users will have completed a lesson; others will not have.
How would I best go about a solution to this? All suggestions and ideas welcome.
Use many-many association and establish relationship among the 3 models.
class Student < ActiveRecord::Base
has_many :progresses
has_many :subjects, through: :progresses
end
class Subject < ActiveRecord::Base
has_many :progresses
has_many :students, through: :progresses
end
class Progress < ActiveRecord::Base
belongs_to :student
belongs_to :subject
end
To be precise use this link to get good understanding on many-many association :)
Third model, say Progress or Completion or Grades or UserLessons... that will belong to user and lesson, and have a field called complete or score or grade....
It's a classic case of many-to-many relation.
User can read multiple lessons
Lessons could be read/complete by multiple users
So to do this is to create another model with column user_id lesson_id as a foreign key for user and lesson and create an additional column completed boolean
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).
I'm new to rails and am done setting up my login system. However, I want someone to be able to make a new blog post and attach it to their account when logged in. How can I attach a post to their user_id as well as list all their previous posts?
Define a model posts ( should have column name user_id )
model Post < ActiveRecord::Base
belongs_to :user
end
In user Model
model User < ActiveRecord::Base
has_many :posts
end
With the above defined associations user_id will be a foriegn key to User model so you can
get all posts by user something like below
User.find(id).posts
There are quite a few different approaches, but John Nunemaker's user_stamp gem is pretty straightforward and simple to get running.
https://github.com/jnunemaker/user_stamp
Just add a creator_id and updater_id to your table, a single line in your ApplicationController, and it'll do the rest!
You can associate models with each other by following this guide on rails associations. One solution for you might be:
class Post < ActiveRecord::Base
end
class User < ActiveRecord::Base
has_many :posts
end
You just have to make sure that your posts table has a column called user_id. Assuming your user table is called users. this will set up a one to many association between users and posts. From a User instance you will be able to do user.posts and get a list of the associated posts for that user.
The guide is much better at explaining this stuff and it's worth your while to read the whole thing--even the parts you don't need right now.
I am working on an application where a user has the ability to leave feedback on another user. Currently, I already have an implemented user model.
Is this considered a self referential association?
This seems a bit wierd to me (how to set it up through Active Record Associations).
How would I go about setting this association up, what type of association is it? Anything I need to watch out for while going about this (tips, suggestions, errors that may come up)?
According to Answers Below Would This be the Way?
(one to many between users and ratings and one to one between ratings and user)
class User < ActiveRecord::Base
has_many :ratings
end
class Ratings < ActiveRecord::Base
belongs_to :user
end
This may not be in Rails/Active Record terms but ORM generic.
I would probably implement this by having a Feedback or Review model and your User model would have one-to-many Feedback/Reviews and the Feedback/Review would have a one-to-one relationship with the User who created it.
EDIT: Response to question update...
You can have an User object on the Ratings model that is the author. My rails experience is minimal but I believe your Ratings model would look like this.
class Ratings < ActiveRecord::Base
belongs_to :user
has_one :user
end
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.