Tag: has many through

获取has_many:x,:through =>:y的问题在控制台中工作

创建新的Rails应用程序(终端) rails new hmt cd hmt 生成模型,脚手架,数据库架构等(终端) rails g model magazine name rails g model reader name rails g model subscription magazine:references reader:references 根据生成的DB模式(终端)创建表 rake db:migrate 检查表是否已创建好(终端) rails c (Rails控制台) Magazine.column_names Reader.column_names Subscription.column_names 在models /(magazine.rb)中指定关系 class Magazine :subscriptions end (reader.rb) class Reader :subscriptions end (subscription.rb) class Subscription < ActiveRecord::Base belongs_to :reader belongs_to :magazine end 添加一些数据(Rails控制台) […]

Rails STI和has_many通过关联不起作用,SQLException:没有这样的表

我有以下型号: 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 这是我的架构: create_table “questions”, force: :cascade do |t| t.string “title” t.text “text” t.datetime “created_at”, […]

has_many通过表单和添加属性来连接表

这是我的第一个stakoverflow问题,对Rails来说还算新。 我搜索过以前的类似问题,但似乎无法想出这个问题。 我有一个与用户和帐户的has_many关系,我在UserAccount模型(连接表)上有一个额外的布尔属性“account_admin”。 我正在尝试在创建新帐户时弄清楚如何设置它。 用户: class User :destroy has_many :accounts, :through => :user_accounts .. end 帐户: class Account :destroy has_many :users, :through => :user_accounts end 用户帐号: class UserAccount < ActiveRecord::Base belongs_to :user belongs_to :account # includes account_admin boolean end 我希望能够创建一个帐户,将用户分配到该帐户,并在一个表单中指定一个account_admin。 到目前为止我有这个允许我选择帐户中的用户,但我不知道如何在这里设置account_admin布尔值。 = simple_form_for @account do |f| = f.input :name = f.input :description = f.association :users, […]

Has_Many:通过或:finder_sql

我已经确定了我想要的东西,但我似乎无法以导轨设计师所寻找的方式获得它。 基本上,我有(请留出多元化/等问题): 人际关系(父母,后代) 我正在努力让所有后代成为一个单亲,以及许多后代的单亲(假设每个后代只有一个父母)。 我可以在模型中以下列方式执行此操作: has_one :parent, :through => :relationships, :foreign_key => :human_id, :source => :source_human has_many :offsprings, :finder_sql => ‘SELECT DISTINCT offsprings.* ‘ + ‘FROM humans offsprings INNER JOIN relationships r on ‘ + ‘r.human_id = offsprings.id where r.source_human_id = #{id}’ 我必须这样做,因为更好的方法: has_many :offsprings, :through => :relationships, :foreign_key => :source_human_id, :source => :human 是不可能的,因为在has_many中忽略了外键(根据这里的文档: […]

ActiveRecord has_many:通过在批量分配上复制计数器缓存

似乎ActiveRecord的counter_cachefunction可能导致计数器缓存递增两次。 我看到这种行为的场景是当我有两个模型具有has_many :through连接模型相互关系(即: Teacher有很多Student通过Classroom )。 使用has_many :through生成的方法直接关联教师和学生(无需手动创建连接记录)时,计数会增加2倍。 示例: teacher.students << Student.create(name: "Bobby Joe")导致teacher.students_count增加2。 请帮助我找到一个缓解或消除此问题的解决方案,同时允许我通过has_many :through关系继续使用内置计数器缓存和批量分配。 我花了很多时间寻找解决方案并将问题解决到一个小的测试应用程序,这是我可以创建的最简单的失败示例。 任何有助于我解决此问题的额外细节都应该在下面。 示例模式和模型: create_table :teachers do |t| t.string :name t.integer :students_count, default: 0 t.timestamps end class Teacher :classrooms end create_table :students do |t| t.string :name t.integer :teachers_count, default: 0 t.timestamps end class Student :classrooms end create_table :classrooms do |t| t.references […]

has_many的问题:通过,缓存,触摸和counter_cache

我有很多has_many:通过我的应用程序中的关系。 我是扩展显示与此相关的信息,例如连接对象的数量。 每当用户更新关系时,联接表都会被修改,我可以抓住这个我的Sweepers。 问题是,连接表条目被删除 ,而不是被销毁 。 如果关系消失了,我没有合理的方法来检测这个,我正在缓存中显示误导性的信息。 一切如:touch => true,或:counter_cache => true partially。 如果更新或创建关系,它会增加。 但是,如果用户删除关系,则没有任 :counter_cache正在崩溃,:触摸不会触发。 垃圾解决方案是在保存主模型时在控制器中调用.touch。 这种作品,但似乎真的非专业。 这应该在模型逻辑中,而不是在控制器中。 我觉得我错过了一些大事,但是我无法理解这一点。 有人可以对这个问题有所了解吗?

源reflection宏无效:has_many:through

我有这样愤怒的联想:融资> – 事件> – 子程序> – 程序。 我希望通过所有程序从程序访问last_financings,因此代码是: class Fcp ‘parent_id’ has_many :subprogram_last_actual_financings, :through => :fcp_subprograms, :source => :last_actual_financings class FcpSubprogram ‘Fcp’, :foreign_key => ‘parent_id’ has_many :events, :foreign_key => ‘fcp_id’ has_many :last_actual_financings, :through => :events, :source => :last_actual_financings class Event ‘Fcp’, :foreign_key => ‘fcp_id’ belongs_to :fcp_subprogram, :class_name => ‘FcpSubprogram’, :foreign_key => ‘fcp_id’ has_many :last_actual_financings, :class_name […]

如何避免has_many:through关系中的重复?

我怎样才能实现以下目标? 我有两个模型(博客和读者)和一个JOIN表,它允许我在它们之间建立N:M关系: class Blog :destroy has_many :readers, :through => :blogs_readers end class Reader :destroy has_many :blogs, :through => :blogs_readers end class BlogsReaders < ActiveRecord::Base belongs_to :blog belongs_to :reader end 我现在想做的是将读者添加到不同的博客中。 但是,条件是我只能将博客添加到博客中。 因此, BlogsReaders表中不得有任何重复项(相同的readerID ,相同的blogID )。 我怎样才能做到这一点? 第二个问题是,如何获得读者尚未订阅的博客列表(例如,填写下拉选择列表,然后可以将读者添加到另一个博客)?

Rails:ActiveRecord :: HasManyThroughSourceAssociationNotFoundError:找不到源关联

我有以下代码(有点简化…… create_table :signatures do |t| t.integer :signer_id t.integer :card_id t.timestamps end 模型看起来像…… class Signature < ActiveRecord::Base belongs_to :card belongs_to :user end class Card :signatures, :foreign_key => “card_id” end class User “Card”, :foreign_key => “sender_id” has_many :received_cards, :class_name => “Card”, :foreign_key => “recipient_id” has_many :signatures has_many :signed_cards, :through => :signatures, :foreign_key => “signer_id” end 我使用rails控制台看到以下错误… ruby-1.9.2-p0 […]

使用嵌套属性在rails中的多对多关系的下拉菜单

我通过多对多关联有三个表:超市,产品和供应。 每个超市都可以容纳许多产品,每个产品都可以在许多超市销售。 该关联是通过Supply-model构建的。 超级市场: class Supermarket :supplies accepts_nested_attributes_for :products end 产品: class Product :supplies accepts_nested_attributes_for :supermarkets end 通过供应协会: class Supply < ActiveRecord::Base attr_accessible :supermarket_id, :product_id belongs_to :supermarket belongs_to :product end 我创建了脚手架,并填充了超市桌。 在我的产品表单中,我想使用一个(或多个)下拉菜单来选择相应的超市名称。 目标是创建一个新产品,同时通过Supply-table创建关联。 如果我想从那里选择相应的超市,那么代码在产品的forms和/或控制器中应该是什么样的?