has-many-through

How to Write a Simple Nested, has_many :through, many-to-many Form in Rails 3.1?

邮差的信 提交于 2019-12-08 05:47:15
问题 I am building a recipe manager as a first rails app. I have a many-to-many, nested set of models based on this quite nice ERD. Ideally, I would like to create a form that allows me to create the recipe in a single form. Also, I would like the user to be able to write/paste the ingredients and steps into a single text field, respectively. In the code below, there is a virtual attribute that parses the cr separated lists in the form to attempt this. I keep getting a "readonly" has_many through

How to save attributes to a has_many :through join table with no existing records to build from

♀尐吖头ヾ 提交于 2019-12-07 19:25:12
问题 I have a form which creates a new Child record and a new Parent record using accepts_nested_attributes_for Children and Parents have a has_many :through association like so: class Child < ActiveRecord::Base has_many :relationships has_many :parents, through: :relationships accepts_nested_attributes_for :parents accepts_nested_attributes_for :relationships, allow_destroy: true end class Parent < ActiveRecord::Base has_many :relationships has_many :children, through: :relationships end class

rails 4 has_many :through not saving associations

旧街凉风 提交于 2019-12-07 16:10:20
问题 I have 2 models with many to many association as follows: class User < ActiveRecord::Base has_many :remark_users, :dependent => :destroy has_many :designated_remarks, :through => :remark_users, :source => :remark end class Remark < ActiveRecord::Base has_many :remark_users, :dependent => :destroy has_many :users, :through => :remark_users accepts_nested_attributes_for :users end And the relationship: class RemarkUser < ActiveRecord::Base belongs_to :remark belongs_to :user end The remarks

Has_Many :Through or :finder_sql

ε祈祈猫儿з 提交于 2019-12-06 23:07:15
问题 I've nailed down what I want, but I can't seem to get it in a way that the rails designers are looking for. Basically, I have (please set aside pluralization/etc issues): Human Relationships (Parent, Offspring) I'm trying to get all the offsprings for a single parent, and the single parent for many offsprings (assume only one parent per offspring). I can do this in the following way in the model: has_one :parent, :through => :relationships, :foreign_key => :human_id, :source => :source_human

Rails associations: How do I limit/scope a has_many :through with multiple self-referencing conditions?

偶尔善良 提交于 2019-12-06 16:09:18
问题 What's the best way to do this? I'm trying something like this, but it feels... wrong. (NOTE: It's all about the :conditions in the has_many association...) class Submission < ActiveRecord::Base belongs_to :checklist has_many :jobs, :through => :checklist, :source => :job, :conditions => ["jobs.archived = false OR jobs.archived_at > #{self.created_at} OR jobs.created_at < #{self.created_at}"] end Rails 3.2.11, Ruby 1.9.3 What I'm trying to do I need to pass multiple conditions on a has_many

Creating joined records using has_many :through

蓝咒 提交于 2019-12-06 14:50:49
Ok I thought I was following this answer pretty well... how to add records to has_many :through association in rails . But clearly not. Model code: class Transaction < ActiveRecord::Base belongs_to :request has_many :transactories has_many :inventories, through: :transactories end class Inventory < ActiveRecord::Base has_many :transactories has_many :transactions, through: :transactories end class Transactory < ActiveRecord::Base belongs_to :inventory belongs_to :transaction end I'm basically trying to match inventories with transactions (people who have stuff, with people who want that stuff)

User has_many :users, :through => :friends - how?

*爱你&永不变心* 提交于 2019-12-06 14:28:02
问题 This is my code: class Friend < ActiveRecord::Base belongs_to :user belongs_to :friend, :class_name => "User", :foreign_key => "friend_id" end class User < ActiveRecord::Base #... has_many :friends has_many :users, :through => :friends #... end When I now start adding users by... user.users << user2 user.save Only the user_id of friend is filled, friend_id is null. Any help? Yours, Joern. 回答1: You need to add the :source attribute to your has_many through association. class User <

How to find records, whose has_many through objects include all objects of some list?

守給你的承諾、 提交于 2019-12-06 13:53:25
问题 I got a typical tag and whatever-object relation: say class Tag < ActiveRecord::Base attr_accessible :name has_many :tagazations has_many :projects, :through => :tagazations end class Tagazation < ActiveRecord::Base belongs_to :project belongs_to :tag validates :tag_id, :uniqueness => { :scope => :project_id } end class Project < ActiveRecord::Base has_many :tagazations has_many :tags, :through => :tagazations end nothing special here: each project is tagged by one or multiple tags. The app

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

瘦欲@ 提交于 2019-12-06 12:00:54
问题 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

rails 4 has_many :through not saving associations

我只是一个虾纸丫 提交于 2019-12-06 03:29:13
I have 2 models with many to many association as follows: class User < ActiveRecord::Base has_many :remark_users, :dependent => :destroy has_many :designated_remarks, :through => :remark_users, :source => :remark end class Remark < ActiveRecord::Base has_many :remark_users, :dependent => :destroy has_many :users, :through => :remark_users accepts_nested_attributes_for :users end And the relationship: class RemarkUser < ActiveRecord::Base belongs_to :remark belongs_to :user end The remarks_controller action that should do the save: # PATCH Save users def save_users @remark = Remark.find(params[