has-many-through

Rails: has_many with extra details?

ぐ巨炮叔叔 提交于 2019-12-09 23:53:33
问题 While I'm not a complete Ruby/Rails newb, I'm still pretty green and I'm trying to figure out how to structure some model relationships. The simplest example I can think of is the idea of "recipes" for cooking. A recipe consists of one or more ingredients and the associated quantity of each ingredient. Assume we have a master list in the database of all ingredients. That suggests two simple models: class Ingredient < ActiveRecord::Base # ingredient name, end class Recipe < ActiveRecord::Base

Dynamically create after_add and after_remove callbacks for has_many or habtm?

前提是你 提交于 2019-12-09 17:25:40
问题 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

Adding and removing from a has_many :through relation

只谈情不闲聊 提交于 2019-12-09 13:11:29
问题 From the Rails associations guide, they demonstrate a many-to-many relationship using has_many :through like so: class Physician < ActiveRecord::Base has_many :appointments has_many :patients, :through => :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end class Patient < ActiveRecord::Base has_many :appointments has_many :physicians, :through => :appointments end How would I create and remove appointments? If I've got a @physician , do I

Rails/ActiveRecord has_many through: association on unsaved objects

岁酱吖の 提交于 2019-12-09 05:37:46
问题 Let's work with these classes: class User < ActiveRecord::Base has_many :project_participations has_many :projects, through: :project_participations, inverse_of: :users end class ProjectParticipation < ActiveRecord::Base belongs_to :user belongs_to :project enum role: { member: 0, manager: 1 } end class Project < ActiveRecord::Base has_many :project_participations has_many :users, through: :project_participations, inverse_of: :projects end A user can participate in many projects with a role

Ruby on Rails has_many through association objects before save

隐身守侯 提交于 2019-12-08 15:59:10
问题 on a Ruby on Rails project I'm trying to access association objects on an ActiveRecord prior to saving everything to the database. class Purchase < ActiveRecord::Base has_many :purchase_items, dependent: :destroy has_many :items, through: :purchase_items validate :item_validation def item_ids=(ids) ids.each do |item_id| purchase_items.build(item_id: item_id) end end private def item_validation items.each do |item| ## Lookup something with the item if item.check_something errors.add :base,

how do I reference and change an extra value in a has many through model

谁说我不能喝 提交于 2019-12-08 11:19:27
问题 I have Lineup and Piece models joined by piece_lineup model (has many through). I added a 'status' column to the piece_lineup model but i can't figure out how to reference that attribute and/or change it. When listing the pieces associated with a lineup, I also want to list the status of the piece as it relates to the lineup. How do I do that? 回答1: It's very simple to get this column. Add to your model: has_many :pieces, through: :piece_lineup, select: "pieces.*, piece_lineup.status as status

has_many through form and adding attribute to join table

别等时光非礼了梦想. 提交于 2019-12-08 09:50:17
问题 This is my first stakoverflow question and fairly new to Rails. I've searched through previous similar questions but can't seem to figure this one out. I have a has_many through relationship with Users and Accounts and I have an extra boolean attribute "account_admin" on the UserAccount model (join table). I'm trying to figure out how to set this when I create a new Account. User: class User < ActiveRecord::Base has_many :user_accounts, :dependent => :destroy has_many :accounts, :through =>

How to implement has_many :through relationship in rails with this example

旧时模样 提交于 2019-12-08 09:35:38
问题 i've been searching through similar questions but i still don't get how implement this relationship. I have of course three models : class Recetum < ActiveRecord::Base attr_accessible :name, :desc, :duration, :prep, :photo, :topic_id has_many :manifests has_many :ingredients, :through => :manifests end class Ingredient < ActiveRecord::Base attr_accessible :kcal, :name, :use, :unity has_many :manifests has_many :recetum, :through => :manifests end class Manifest < ActiveRecord::Base attr

Rails associations has_many through ban/archive solution?

半腔热情 提交于 2019-12-08 06:40:22
问题 I'm new in Rails and am working on a problem. I have two tables: Shoes and Socks A Shoe can have many Socks, but only one active Sock. Other Socks are inactive. All Socks are also unique with unique patterns. I want to make sure I don't have socks with the same pattern. I think I can do this three ways 1) Using an additional column in table socks to represent the active sock class Shoe < ActiveRecord::Base has_many :socks end class Sock < ActiveRecord::Base belongs_to :shoe end class

Connecting database view with polymorphic model in rails

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 06:22:38
问题 I have the following setup which allows a user to have multiple projects and each project can have multiple tasks. A user can favourite multiple projects. class User < ActiveRecord::Base has_many :projects has_many :tasks, :through => :projects has_many :favourites has_many :favourite_projects, :through => :favourites, :source => :favourable, :source_type => "Project" has_many :favourite_tasks, :through => :favourite_projects, :source => :tasks ... end class Project < ActiveRecord::Base