has-many-through

Detecting changes in a rails has_many :through relationship

戏子无情 提交于 2020-01-13 08:12:10
问题 I have a model that has a number of has_many, and has_many :through model relationships. For example, in my User class I have: has_many :languages, through: :profile_languages What I would like is to be able to detect when these are added or removed using the 'User.changes' function, which returns an array of attributes that have been changed when called with the User.language_ids= function. Has anyone else tried to do this, or have experience with this? Info on the ActiveModel changes

Rails 4: counter_cache in has_many :through association with dependent: :destroy

北战南征 提交于 2020-01-11 09:54:09
问题 Although similar questions have already been asked: counter_cache with has_many :through dependent => destroy on a "has_many through" association has_many :through with counter_cache none of them actually addresses my issue. I have three models, with a has_many :through association : class User < ActiveRecord::Base has_many :administrations has_many :calendars, through: :administrations end class Calendar < ActiveRecord::Base has_many :administrations has_many :users, through:

incorrect database records created for rails 3 has_many :through association

自古美人都是妖i 提交于 2020-01-06 19:45:13
问题 I have a has_many :through association. Players have many Teams and Teams have many Players. The join model, Affiliation, belongs to Players and Teams, and also has a year attribute to keep track of a player's team affiliation (or employment) from year to year. I can't seem to figure out the right way to build an association based on the following rules: Create a new player. Associate a team that may be new or existing. So find it or create it, but only create it if the player is saved. The

Problems getting has_many :x, :through => :y to work in the console

感情迁移 提交于 2020-01-05 12:16:12
问题 Create new Rails app (terminal) rails new hmt cd hmt Generate models, scaffolding, DB schema, etc (terminal) rails g model magazine name rails g model reader name rails g model subscription magazine:references reader:references Make tables based on generated DB schema (terminal) rake db:migrate Check if tables are created ok (terminal) rails c (Rails console) Magazine.column_names Reader.column_names Subscription.column_names Specify relationships in models/ (magazine.rb) class Magazine <

Invalid source reflection macro :has_many :through

点点圈 提交于 2019-12-31 03:14:41
问题 I have such angry associations: financings >- events >- subprograms >- programs. I want to get acces to last_financings from programs through all of them so code is: class Fcp < Program has_many :fcp_subprograms, :foreign_key => 'parent_id' has_many :subprogram_last_actual_financings, :through => :fcp_subprograms, :source => :last_actual_financings class FcpSubprogram < Program belongs_to :fcp, :class_name => 'Fcp', :foreign_key => 'parent_id' has_many :events, :foreign_key => 'fcp_id' has

Drop-Down-Menu for Many-to-Many relation in rails using nested attributes

隐身守侯 提交于 2019-12-30 05:24:08
问题 I have three tables via many-to-many-association: Supermarket, Product and Supply. Each Supermarket can hold many products and each product can be sold in many supermarkets. The association is build via the Supply-model. Supermarket: class Supermarket < ActiveRecord::Base attr_accessible :name, :address, :products_attributes has_many :supplies has_many :products, :through => :supplies accepts_nested_attributes_for :products end Product: class Product < ActiveRecord::Base attr_accessible :name

Having trouble in updating join model extra attributes in has_many through association - Rails

帅比萌擦擦* 提交于 2019-12-25 04:10:35
问题 I am still a learner of Rails, and am making a simulation game of Google Adwords for training usage. Inside the game (I may only explain the related part of this question), I have three models, Adgroup (called KeywordAdgroup in my code), Keyword, AdgroupKeyword , and also a Game model that has a game_id in it. The relationship of the three models is a has_many through: assciation, which Adgroup has many Keywords through AdgroupKeyword, and Keyword has many Adgroups through AdgroupKeyword. Now

has_many through association with existing object / ActiveRecord

人走茶凉 提交于 2019-12-24 18:15:53
问题 Given models class Composition < ActiveRecord::Base attr_accessible :content has_many :compositions_tags has_many :tags, :through => :compositions_tags end class Tag < ActiveRecord::Base attr_accessible :text has_many :compositions_tags has_many :compositions, :through => :compositions_tags validates_uniqueness_of :tag, only: [:create, :update], message: "already taken" end class CompositionsTag < ActiveRecord::Base belongs_to :composition belongs_to :tag end Now, for example I do Composition

Rails STI and has_many through association not working, SQLException: no such table

陌路散爱 提交于 2019-12-24 08:34:46
问题 I have the following models: class Test < ApplicationRecord end class Exam < Test end class Practice < Test has_many :relations has_many :questions, through: :relations end class Relation < ApplicationRecord belongs_to :practice belongs_to :question end class Question < ApplicationRecord has_many :relations has_many :practices, through: :relations end And this is my schema: create_table "questions", force: :cascade do |t| t.string "title" t.text "text" t.datetime "created_at", null: false t

Rails: build for difference between relationships

狂风中的少年 提交于 2019-12-24 06:07:53
问题 A doc has many articles and can have many edits . I want to build an edit for each article up to the total number of @doc.articles . This code works with the first build (i.e., when no edits yet exist). def editing @doc = Doc.find(params[:id]) unbuilt = @doc.articles - @doc.edits unbuilt.reverse.each do |article| @doc.edits.build(:body => article.body, :article_id => article.id, :doc_id => @doc.id) end end But when edits already exist it'll keep those edits and still build for the @doc