has-many-through

How do I save and update attributes in a join table in Rails 4 HMT association?

梦想与她 提交于 2019-12-04 15:58:17
I have a has_many through join table setup for a recipe app where Ingredient and Meal connect through MealIngredient . Within MealIngredient , I have meal_id , ingredient_id , and amount . My question is: How can I save and update the amount column in the meal form? My form field for adding an ingredient looks like this: <% Ingredient.all.each do |ingredient| %> <label> <%= check_box_tag "meal[ingredient_ids][]", ingredient.id, f.object.ingredients.include?(ingredient) %> <%= ingredient.name %> </label> <br /> <% end %> How do I save the amount for each ingredient? I am referencing this

Issues with has_many :through, cache, touch and counter_cache

吃可爱长大的小学妹 提交于 2019-12-04 13:33:41
问题 I have a lot of has_many :through relations in my app. I am extensivley showing informations related to this, such as number of connected objects. Whenever user updates the relation, join table is modified, and I can catch this my Sweepers. The problem is, that join table entries are deleted , not destroyed . If relation is gone, I have no resonable way to detect this, and I am displaying misleading informations from the cache. Everything like :touch => true, or :counter_cache => true works

MySQL: 4 Table “has-many-through” Join?

不羁的心 提交于 2019-12-04 10:28:49
Let's say I have the following 4 tables (for examples' sake): Owners, Trucks, Boxes, Apples. An owner can have many trucks, a truck can have many boxes and a box can have many apples. Owners have an id. Trucks have an id and owner_id. Boxes have an id and truck_id. Apples have an id and box_id. Let's say I want to get all the apples "owned" by an owner with id = 34. So I want to get all the apples that are in boxes that are in trucks that owner 34 owns. There is a "hierarchy" if you will of 4 tables that each only has reference to its direct "parent". How can I quickly filter boxes while

Elegantly selecting attributes from has_many :through join models in Rails

淺唱寂寞╮ 提交于 2019-12-04 09:57:36
问题 I'm wondering what the easiest/most elegant way of selecting attributes from join models in has_many :through associations is. Lets say we have Items, Catalogs, and CatalogItems with the following Item class: class Item < ActiveRecord::Base has_many :catalog_items has_many :catalogs, :through => :catalog_items end Additionally, lets say that CatalogueItems has a position attribute and that there is only one CatalogueItem between any catalog and any item. The most obvious but slightly

Simple_Form Association with has_many :through extra field

六月ゝ 毕业季﹏ 提交于 2019-12-04 08:30:43
问题 I have two models, Developers and Tasks, class Developer < ActiveRecord::Base attr_accessible :address, :comment, :email, :name, :nit, :phone, :web has_many :assignments has_many :tasks, :through => :assignments end class Task < ActiveRecord::Base attr_accessible :description, :name, :sprint_id, :developer_ids has_many :assignments has_many :developers, :through => :assignments end class Assignment < ActiveRecord::Base attr_accessible :accomplished_time, :developer_id, :estimated_time,

ActiveRecord has_many :through duplicating counter caches on mass assignment

浪尽此生 提交于 2019-12-04 06:42:52
It seems ActiveRecord's counter_cache feature can result in a counter cache being incremented twice. The scenario in which I am seeing this behavior is when I have two models that have a has_many :through relationship with each other through a join model (ie: Teacher has many Student through Classroom ). When using the has_many :through generated methods to associate Teacher and Student directly (without manually creating the join record) the count goes up 2 times. Example: teacher.students << Student.create(name: "Bobby Joe") causes teacher.students_count to increment by 2. Please help me

Rails has_many :through with conditions and building associations

我怕爱的太早我们不能终老 提交于 2019-12-04 05:11:13
I'm having problems building an association that is a has_many :through with conditions. I have this model: class Contact < AR has_many :group_contacts has_many :groups, :through => :group_contacts, :conditions => {:groups => {:published => true}} end problem happens when I try to instantiate a group from a contact. With the above syntax, I get an error: contact.groups.build => ActiveRecord::UnknownAttributeError: unknown attribute: groups But when I use the following syntax it works: has_many :groups, :through => :group_contacts, :conditions => ['groups.published = ?', true] contact.groups

Dynamically create after_add and after_remove callbacks for has_many or habtm?

寵の児 提交于 2019-12-04 04:42:24
Is there a way to dynamically add after_add and after_remove callbacks to an existing has_many or has_and_belongs_to_many relationship? For example, suppose I have models User , Thing , and a join model UserThingRelationship , and the User model is something like this: class User < ActiveRecord::Base has_many :user_thing_relationships has_many :things, :through => :user_thing_relationships end I'd like to be able to, in a module that extends User , add :after_add and :after_remove callbacks to the User.has_many(:things, ...) relationship. I.e., have something like module DoesAwesomeStuff def

How do I create an association with a has_many :through relationship in Factory Girl?

☆樱花仙子☆ 提交于 2019-12-03 21:11:33
In my models I have the following setup: class User < ActiveRecord::Base has_many :assignments has_many :roles, :through => :assignments end class Role < ActiveRecord::Base has_many :assignments has_many :users, :through => :assignments end class Assignment < ActiveRecord::Base belongs_to :user belongs_to :role attr_accessible :role_id, :user_id end In my factory.rb file I have: FactoryGirl.define do factory :user do sequence(:username) { |n| "user#{n}" } email { "#{username}@example.com" } password 'secret' password_confirmation 'secret' factory :admin do ... end end factory :role do name

Rails has_many through polymorphic counter cache

此生再无相见时 提交于 2019-12-03 20:22:50
I have two models I link together using a polymorphic has_many through association and I would like to add a counter_cache but it seems Rails/ActiveRecord does not support this feature out of the box. class Classifiable < ActiveRecord::Base has_many :classifications, :as => :classifiable, :foreign_key => :classifiable_id end class Taxonomy < ActiveRecord::Base has_many :classifications, :as => :taxonomy, :foreign_key => :taxonomy_id end class Question < Classifiable has_many :categories, :through => :classifications, :as => :classifiable, :source => :taxonomy, :source_type => "Category" end