Has_many through - Rails 5 - ruby-on-rails

I’m a rails newbie here and have this issue.
I have 3 models: Group, User, Subject, between Group and Subject I have a has_many through relation called GroupSubjects it works fine, the next is GroupUsers with a has_many through relation and works fine too, as new at this (like 1 mint learning rails) I want to assign a subject, with another view, not the new or edit templates, and with this view, and make the relation between Groups and Subjects, I want to save the user_id, group_id, and due date, in the GroupSubjects table.
I really need help, this has been a problem for at least 3 days.
Thanks for your help

Related

Fun Rails ActiveRecord Join with attributes

I'm feeling rusty on my basic rails and working on a simple music stats service.
class User
has_many :monthly_top_artists # this model also has play_count and month attrs- not just a join
has_many :top_artists, through: monthly_top_artists, source: :artist
I've basically written my own #monthly_top_artists query method which queries for a specific month's artists. right now i'm working with a line like this:
current_top_artists = MonthlyTopArtist.where(user_id: id, month: time_range)
.joins(:artist).select('month,name,play_count,user_id,artist_id,monthly_top_artists.updated_at')
The purpose of this is to load up my MonthlyTopArtist model with all the artist data as well- but am I just forgetting some basic rails/active record methods that can handle this for me? I could also use user.top_artists with the code above, but this would give me the same problem- the monthly statistics are in another table. Am I overthinking this in trying to limit my db queries? I realize that writing my own getter method is a little bit more than vanilla rails but this join + selecting attributes not defined on my MTA model is making me think there is something bigger wrong with my schema (or i'm just forgetting something).

Referencing attributes across multiple tables in Rails

Let me try and condense my question:
I want to display data from multiple tables in a particular view. I want to list every person I have in a "People" table, and append there job on an "Affiliations" table listed with their company from an "Employers" table. Affiliations should belongs_to People and Employers, and Employers and People have_many Affiliations. What would the migration and controller look like?
I'm not entirely sure if this is the same as what you're asking, but using has_many and belongs_to may be a better solution. Using these associations would allow an employee to have many employers and then simply get the most recent one.
Please feel free to correct me if this isn't what you are asking.

Rails 3.1 Active Record associations possibly with nested forms

I have a question regarding a simple app I am developing in Rails 3.1, but I am not sure of how to define the associations and am looking for advice on the best way to achieve the following.
I have tapes, each tape contains a barcode.
I have boxes, each box has a name
What i need to track is the act of associating the two , each day up to 24 tapes will be placed into a Box, so i need a way of creating a new association for existing boxes and existing tapes on a form, and ideally track some extra information.
The tapes are on a 5 week cycle, so after 5 weeks a tape will ( possibly ) be associated with a new box.
I am not sure how to model the associations, any help or input would be appreciated
Do you need to track which box it used to belong to? If not, this is a simple belongs_to/has_many association.
class Tape
belongs_to :box
end
class Box
has_many :tapes
end
If you need to track past boxes, then you'll need a join-model:
class WhateverYouWant
belongs_to :box
belongs_to :tape
end
and change the box/tape associations to use ":through"

Adding categories to a Ruby on Rails application

I've had some issues with this before when creating applications and I think I'm starting to run into it again, hence I'm asking this on StackOverflow to save me a lot of time.
I've spent the last few weeks setting up a perfected product model for my system. The model performs exactly as I want it to and has several complex features (such as search via sunspot). I wanted to setup the category to product structure before I started this heavy development - however struggling with this kind of thing was just putting me off creating the application so I got straight into the product structure.
Now I've got the product model setup - what would be the easiest way to add a category ownership to encompass the products? (All products have a category_id column which store their father category id)
My plan is to have the category index to be a list of all the categories, the category show to be a list of the products inside that category and the product show being the view of the actual product. This would eliminate the product index and so I'll have to come up with a way to port the search feature (sunspot) from my index view to the category show somehow.
As for the actual listing of the products - I assume I'll have to do some kind of partial? (I don't know a lot about it).
Most basically, my relationships are planned to be:
category:
has_many :products
product:
has_one :category
My products then have a category_id column to store the ID of it's parent category.
Any tips on how to accomplish the relationships (category show to list the products etc)?
Best Regards,
Joe
Relationships like the one you're wanting are built into ActiveRecord support. Understanding the model relationships in Rails is critical to doing anything in Rails that's non-trivial, so study up.
Also, the relationship you're looking for is something like:
product:
belongs_to :category
category:
has_many :products

Rails 3 sorting through parent association

I have a fairly straightforward question in Rails 3 to which I can't seem to find the answer:
Let's say I have 2 models: Customer, Project.
A Customer has_many projects
Project belongs_to customer
Now I want to sort a list of projects by "customer name" using Active Record (doing it with a Ruby array is easy enough, but I imagine this will get problematic once the number of project records grows).
How do I go about doing this with ActiveRecord?
Project.joins(:customer).order('customers.name')

Resources