has-many-through

dependent => destroy on a “has_many through” association

本秂侑毒 提交于 2019-12-17 17:38:40
问题 Apparently dependent => destroy is ignored when also using the :through option. So I have this... class Comment < ActiveRecord::Base has_many :comment_users, :dependent => :destroy has_many :users, :through => :comment_users ... end ...but deleting a Comment does not result in the associated comment_user records getting deleted. What's the recommended approach, then, for cascade deletes when using :through? Thanks 回答1: Apparently :dependent is not ignored! The real issue was that I was

Rails has_many through form with checkboxes and extra field in the join model

拜拜、爱过 提交于 2019-12-17 08:51:08
问题 I'm trying to solve a pretty common (as I thought) task. There're three models: class Product < ActiveRecord::Base validates :name, presence: true has_many :categorizations has_many :categories, :through => :categorizations accepts_nested_attributes_for :categorizations end class Categorization < ActiveRecord::Base belongs_to :product belongs_to :category validates :description, presence: true # note the additional field here end class Category < ActiveRecord::Base validates :name, presence:

Rails has_many :through Find by Extra Attributes in Join Model

橙三吉。 提交于 2019-12-17 03:46:46
问题 New to both Ruby and Rails but I'm book educated by now (which apparently means nothing, haha). I've got two models, Event and User joined through a table EventUser class User < ActiveRecord::Base has_many :event_users has_many :events, :through => :event_users end class EventUser < ActiveRecord::Base belongs_to :event belongs_to :user #For clarity's sake, EventUser also has a boolean column "active", among others end class Event < ActiveRecord::Base has_many :event_users has_many :users,

Cannot use group with has_many through in Rails 5

杀马特。学长 韩版系。学妹 提交于 2019-12-13 20:27:01
问题 I have the following associations: class Student < ApplicationRecord has_many :people_schools has_many :schools, through: :people_schools end class PeopleSchool < ApplicationRecord belongs_to :student belongs_to :school end class School < ApplicationRecord has_many :people_schools has_many :students, through: :people_schools end I am trying to get a list of students organized by their school. I have tried the following: Student.joins(:schools).all.group('schools.name') but I get the following

Rails 4: child record ID is saved but not its attributes, using nested form with has_many through relationship

笑着哭i 提交于 2019-12-13 06:53:48
问题 I have 3 models with a has_many through relationship: Food (eg: Chocolate), Sub (Chocolate food substitute), Joint (joint table). Say @food = Food.find(1); The has_many through relationship allows me to do @subs = @food.subs which return all substitutes associated with @food. This work fine, however only the Sub id is saved and not its attributes which are :name and :description as you can see it returned nil when trying to save @food.subs in my create action in my controller: => #

Form to create entries in many-to-many relationship with nested models

℡╲_俬逩灬. 提交于 2019-12-13 05:25:15
问题 My application consists of these five models: A supermarket can have different categories of products and the products in these categories can be produced by several brands . Now I want to have one (or two) Selection -field(s) in my supermarket -form in which I can select one element in Category with its name appearing and one or more element(s) in Brand with its name appearing, so this could be stored in Origin . I think I could use collection_select , but how do I utilize it here? class

Order By Count on has_many :through

喜夏-厌秋 提交于 2019-12-13 04:29:45
问题 I use Rails 4. I have an application where i have a many-to-many relationship : class User < ActiveRecord::Base has_many :relationshipfollows, :foreign_key => "follower_id", :dependent => :destroy has_many :following, :through => :relationshipfollows, :source => :followed end class Project < ActiveRecord::Base has_many :relationshipfollows, :foreign_key => "followed_id", :dependent => :destroy has_many :followers, :through => :relationshipfollows, :source => :follower end class

has_many :throught not INSTERING INTO database from form

血红的双手。 提交于 2019-12-12 06:24:07
问题 I have tried all of the solutions to similar problems and haven't gotten this one figured out. I have a has_many :through relationship between 'Clinician', and 'Patient' with a joined model 'CareGroupAssignment'. I would like to have a patient be able to have multiple clinicians associated with it and clinicians will have multiple patients . When I submit my form this is logged: Started POST "/patients" for 127.0.0.1 at 2015-09-02 14:56:38 -0700 Processing by PatientsController#create as HTML

has_many :through creating child after_save --> ActionView::Template::Error

梦想的初衷 提交于 2019-12-12 06:19:10
问题 I have three models: List, Food, and Quantity. List and Food are associated through Quantity via has_many :through. The model association is doing what I want, but when I test, there is an error. test_valid_list_creation_information#ListsCreateTest (1434538267.92s) ActionView::Template::Error: ActionView::Template::Error: Couldn't find Food with 'id'=14 app/views/lists/show.html.erb:11:in `block in _app_views_lists_show_html_erb__3286583530286700438_40342200' app/views/lists/show.html.erb:10

Rails admin has_many :through relationship causing infinite rendering of templates when editing resource

我的梦境 提交于 2019-12-12 04:53:51
问题 I have a rails app with a has_many :through model like so: class Blog < ActiveRecord::Base has_many :blog_categorizations, dependent: :destroy has_many :categories, :through => :blog_categorizations accepts_nested_attributes_for :categories, allow_destroy: true end class Category < ActiveRecord::Base has_many :blog_categorizations, dependent: :destroy has_many :blog, :through => :blog_categorizations accepts_nested_attributes_for :blog, allow_destroy: true end class BlogCategorization <