I'm trying to have read and unread status on a particular post by the specific user using Rails 5. I'm having post_users join table in which post_id and user_id will be stored. Posts and users associated with many to many (has and belongs to many). I don't have a model for the post_users table. How to check the join table in the controller?
Related
I have database with different tables. But I can't get access to some tables, and I'm curious why? For example, there is Projects table and Users table in database, and I need to get list of all users that are in specific Project by Id. These tables have many to many relationship, so there is joining table with ProjectId and UserId. It's logic to use some Join to get users that I need from table Users with help of this joining table from db. But how can I make Join when my DbContext doesn't have joining table? How can I get access to joining table using DbContext, or maybe there are some different ways to get list of users by project id?
If someone have similar problem, here is a simple way to use all the data from collections in model
var project = _db.Projects.Include(x => x.Tickets).ThenInclude(y => y.Users).SingleOrDefault(p => p.Id == id);
so after that you gonna have your model and all the data from collections that you need
ProjectUser - is joining table, use for link two tables. This tables must have (usually) Collection for access to this many-to-many data.
So you can will have collection of users for this project. And doing filtering by users from your Ticket (example from image).
Yes, exists few different ways - setting with ef fluent, or attribute, or dbcontext. Better to find complete guide for many-to-many setting in your application.
I would like to retrieve all of one table and all joined records in another.
I would like to have all columns from both tables
This is extremely simple in SQL
e.g.
SELECT *
FROM students
JOIN teachers
ON students.id = teachers.student_id
How can I do the same in rails?
I've tried variations on
Student.includes(:teacher)
and
Student.joins(:teacher).includes(:teacher)
The join is working, but I cannot access columns from Teacher table
Note that the end goal is simply to be able to create an instance variable in the controller so that I can access both student and teacher data in the view
Student.includes(:teacher) will return ActiveRecord::CollectionProxy which means if take particular object in this collection, it will be Student class object.
Unlike sql query fired and returning data from 2 tables, it does not work same in rails, you get data only from students column which will relate associated record in teachers table because it represent Student model.
You can access further teachers data like,
students = Student.includes(:teacher)
students.last.teacher.name
In above no new query will get fired in database when you call teacher association on object
Let's say I am building a potluck organizing app. I have a join table tracking Participants and Events (MTM), I also have a Dishes table with dishes that are unique and associated to each Participant (OTM). Participants will sign up to events and agree to bring one or more of their Dishes.
The ParticipantEvents join table will track the RSVP but what is the best way to track which dishes the Participant is contributing to that Event?
Should I create a join table between the Dishes and the Events?
Should I create a join table between the ParticipantEvents join table and the Dishes table (ParticipantEventsDishes)?
Is there a better way?
The case looks like a three way (ternary) relationship to me. So I would create a three way join table, with three foreign keys.
Promised (DishID, EventID, ParticipantID)
Now you can join this with all three tables to produce the details for all participants. Note that a participant can promise more than one dish for an event.
Is there any way to join the Identity user table with any other table of our own in MVC 5. Because fetching the data is really difficult. If we want to retrieve user product on the bases of some roles and then their products. So its a join of three tables but join of Products table with other two tables i don't know how to use this. Because User and Roles tables are identity table and belongs to IdentityDbContext.
So I am working on an MVC project to put to work the studying I have been doing. I am wrestling with the concept of Database Table relationships and foreign keys. I am working on a simple ecommerce site (displays products, shopping cart, user accounts..etc).
I have the following tables to start out with:
1) Products
2) Categories
I setup the Products and Categories tables to have a ProductId and CategoryId respectively. In my MySQL db, I created a FK on the Products Table to relate to the CategoryId field on the Categories table (I am not sure this was correct to begin).
My expectations for the way the database would handle the table relationship: I didn't want the DB to do anything with the products table if I deleted a category out of the Categories table, or vise versa. The only thing would be that the category field in a Product would be blank (or default) if their category was removed.
Finally, do I have to do anything in my entity classes such as in the Products class, add the ProductId to the Category.ProductId?
Eventually, when I Orders and Users to the project, I can see a relationship where each user -> many orders -> each order has many products -> and each product is in one category.
But I am having a hard time understanding how or if I should be setting up a Foreign key relationship in the two current tables of Products and Categories and if so how to setup my entity class in relation to that FK.
Any advice.
It has been my experience (with L2S) that you don't want to specify any relationships between tables. I have simply done the PK, FK logic myself. This keeps the L2S generated objects simple and is most likely the way you worked with them before in SQL. That, at least, is the case for me.