Including attributes from a join model in a seperate form field - ruby-on-rails

I have an association setup where a Track can belong to multiple Releases through ReleaseTracks:
class Release < ApplicationRecord
has_many :release_tracks, dependent: :destroy
has_many :tracks, through: :release_tracks
end
class ReleaseTrack < ApplicationRecord
# disc, side, number
belongs_to :release
belongs_to :track
belongs_to :album
end
class Track < ApplicationRecord
has_many :release_tracks, dependent: :destroy
has_many :releases, through: :release_tracks
validates_uniqueness_of :name, scope: [:release_id, :album_id]
end
As you can see, the ReleaseTrack has three attributes disc, side, number to correctly identify the correct placement for a track on any specific release. I was wondering in the instance of creating a new track in a release form, how could I also save those three attributes?

Related

when to use polymorphic associations vs STI vs another table in rails

I have taken a look at Polymorphic associations and STI but I don't really know if they apply to my specific use case. In my app I have the following relevant classes:
class Restaurant < ApplicationRecord
belongs_to :owner, class_name: "User"
has_many :menus
has_many :dishes
has_many :categories
has_many :bookings
has_many :simple_bookings
mount_uploader :photo, PhotoUploader
end
class Dish < ApplicationRecord
has_many :menu_dishes
has_many :menus, through: :menu_dishes
belongs_to :category
belongs_to :restaurant
mount_uploader :photo, PhotoUploader
end
class Menu < ApplicationRecord
belongs_to :restaurant
has_many :bookings
has_many :categories
has_many :menu_dishes
has_many :dishes, through: :menu_disheshas_many :menu_dishes
end
class MenuDish < ApplicationRecord
belongs_to :dish
belongs_to :menu
end
So far the user flow is the following:
A restaurant owner signs up and creates restaurant dishes. Then this owner creates a menu containing different MenuDishes.
Afterwards, users can search for different menus and book them.
My problem is the following:
I want to implement a feature where owners can add up to 3 MenuDishOptions (that belong to MenuDishes) so that a user can change each MenuDish with the available MenuDishOption for that MenuDish.
In other words, I want each MenuDish to contain many MenuDishOptions. Once a MenuDishOption is selected, the MenuDish passes to be a MenuDishOption and the selected MenuDishOption to a MenuDish.
Therefore the MenuDishOption class would look something like this:
class MenuDishOption < ApplicationRecord
belongs_to :menu_dish
has_one :dish
belongs_to :menu, through: menu_dishes
end
The MenuDish class would be updated to:
class MenuDish < ApplicationRecord
has_many :menu_dish_options
belongs_to :menu
belongs_to :dish
end
Please let me know if I need to share more info and thanks a million to anyone that takes the time to help this newbie.

How to correctly set up model associations

I want a user to be able to create and save or choose an existing client to add to an invoice. There can only be one client per invoice.
I currently have 3 models users invoices and items
I am using a simple has_many relationship currently but I am getting confused now that I want to add a new table clients. I was hoping I could get some advice on what association to use.
My current associations
class User < ActiveRecord::Base
has_many :invoices
class Invoice < ActiveRecord::Base
belongs_to :user
has_many :items, :dependent => :destroy
class Item < ActiveRecord::Base
belongs_to :invoice
So I was thinking to do something simple like adding has_many :clients
to users, add has_one :client to invoices and add the table clients
class User < ActiveRecord::Base
has_many :invoices
has_many :clients
class Client < ActiveRecord::Base
belongs_to : user
class Invoice < ActiveRecord::Base
belongs_to :user
has_many :items, :dependent => :destroy
has_one :client
Would this work? Is there a better way?
It is very rare that you will use has_one. In your case the following models make more sense:
class User < ActiveRecord::Base
has_many :invoices
end
class Invoice < ActiveRecord::Base
belongs_to :user
belongs_to :client
has_many :items, :dependent => :destroy
end
class Item < ActiveRecord::Base
belongs_to :invoice
end
class Client < ActiveRecord::Base
has_many :invoices
has_many :items, through: :invoices
end

Insert child records all at once

I have these models:
class Project < ActiveRecord::Base
has_many :task_links, -> { includes(:task).order("tasks.name") }, dependent: :destroy
has_many :tasks, through: :task_links
class TaskLink < ActiveRecord::Base
belongs_to :project
belongs_to :task
class Task < ActiveRecord::Base
has_many :task_links, dependent: :destroy
has_many :projects, through: :task_links
I want to create an after_create callback that will automatically create all task_links for all active projects and the newly created task. I can do this by looping over the active projects and creating a task_link for each, but I'm wondering if there is a nicer way to do this? Preferably with one big insert command instead of xxx.
I'm not sure why you'd need to use an after_create to do this, because I think a before_create would work quite well. With a before_create, you can save all of your task_links when your new task is saved.
class Task < ActiveRecord::Base
has_many :task_links, dependent: :destroy
has_many :projects, through: :task_links
before_create :create_task_links
def create_task_links
# I'm assuming that the where below satisfies "all active projects"
Project.where(active: true).find_each do |proj|
# build only creates objects but does not save them to your database
self.task_links.build(project: proj)
end
end
end

many to many relation by Users and Files and ownership of it in rails

How can I add ownership in many to many relationships?
For example like this models.
class User < ActiveRecord::Base
has_many :editabilities, dependent: :destroy
has_many :files, through: :editabilities
end
class File < ActiveRecord::Base
has_many :editabilities, dependent: :destroy
has_many :users, through: :editabilities
end
class Editabilities < ActiveRecord::Base
belongs_to :user
belongs_to :file
end
And I want to add a one-to-many relationship to User-and-Files.
At first I thought it is best to add owner boolean column to Editabilities, but I have no idea how to handle it.
Secondly I thought if I make a new junction model Ownerships, then I can handle it same way as Editabilities. But I've got a uninitialized constant User::Ownership when I tried it with code like this.
class User < ActiveRecord::Base
has_many :editabilities, dependent: :destroy
has_many :ownerships, dependent: :destroy
has_many :files, through: :editabilities
has_many :owned_files, through: :ownerships, source: :file
end
class File < ActiveRecord::Base
has_many :editabilities, dependent: :destroy
has_many :ownerships, dependent: :destroy
has_many :users, through: :editabilities
has_one :owner, through: :ownerships, source: :user
end
class Editabilities < ActiveRecord::Base
belongs_to :user
belongs_to :file
end
class Ownerships < ActiveReord::Base
belongs_to :user
belongs_to :file
end
How can I implement a feature like this?
The only problems I see here are the classes Editabilities and Ownerships. By Rails convention model class names should be singular, not plural.
class Editability < ActiveRecord::Base
belongs_to :user
belongs_to :file
end
class Ownership < ActiveReord::Base
belongs_to :user
belongs_to :file
end
One way to quickly check your class names is by checking the result of the classify function:
> "editibilities".classify
=> "Editibility"
> "ownerships".classify
=> "Ownership"
The rest of the associations all look correct.
Your class should be named Ownership and not Ownerships.
ActiveRecord class names are normally singular and table names are plural.
Easier solution seems to add belongs_to association to File model. Because 1 File can have only 1 owner.
class File < ActiveRecord::Base
...
belongs_to :owner, class_name: 'User'
end
You will need to add owner_id column to files table.

rails :dependent=>:destroy being ignored

My association option does not appear to be honored.
class ClassRoom < ActiveRecord::Base
has_many :class_assignments, :dependent => :destroy
has_many :people, :through=>:class_assignments
class Person < ActiveRecord::Base
has_many :class_assignments, :dependent => :destroy
has_many :class_rooms, :through=>:class_assignments
class ClassAssignment < ActiveRecord::Base
belongs_to :person
belongs_to :class_room
That is to say, when either a person, or a class room is deleted, the record in the join table/model should also be deleted.
However, ClassRoom.last.destroy destroys the ClassRoom, but no any of it's associated ClassAssignments.
I know :dependent=>:destroy is ignored when :through is used, but I should be able to use it on the join model right?
try
class ClassRoom < ActiveRecord::Base
def before_destroy
self.class_assignments.destroy_all
end
same for person

Resources