Tag: has many through

Rails4中的嵌套简单表单 – 有很多通过,保存多个记录

通过关系,我有一个标准的has_many。 通过连接表交互,人类拥有许多兽人。 互动只是一个表格和模型; 没有控制器或视图。 使用Rails 4中的simpleform gem,我想从人体页面创建一个表单,以便从所有兽人的池中选择多个兽人。 提交后,我希望它在交互表中创建/更新尽可能多的记录,每个记录都包含人员ID,并且选择了多个orc id。 : AKA列表符号 从一端制作表格(人类) 列出表格中的所有兽人 从该列表中选择多个兽人 使用human_id和orc_id将多个记录保存到交互表中,因为从该列表中选择了兽人。 (human_id在这些记录中将是相同的,因为它从给定的人类表单页面开始) 我将尽可能多地编写整个故事的代码。 请随时要求澄清,并解决任何错误,以实现这一点。 表 humans integer “id” interactions integer “human_id” integer “orc_id” index [“human_id”, “orc_id”] # This is the primary key. no normal id. # Is it better to have a primary id for this join table, or does it […]

Rails / ActiveRecord has_many through:未保存对象的关联

让我们使用这些类: 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 user可以作为member或manager参与许多projects 。 连接模型称为ProjectParticipation 。 我现在在使用未保存对象上的关联时遇到问题。 以下命令的工作方式与我认为它们应该有效: # first example u = […]

Rails has_many通过使用source和source_type别名来处理多种类型

所以这是一个示例类 class Company < ActiveRecord::Base has_many :investments has_many :vc_firms, through: :investments, source: :investor, source_type: 'VentureFirm' has_many :angels, through: :investments, source: :investor, source_type: 'Person' end @ company.angels和@ company.vc_firms按预期工作。 但是,我如何拥有由两种源类型组成的@ company.investors? 这适用于投资表中投资者专栏的所有多态? 或者也许是使用范围合并所有source_type的方法? 投资模式如下: class Investment < ActiveRecord::Base belongs_to :investor, polymorphic: true belongs_to :company validates :funding_series, presence: true #, uniqueness: {scope: :company} validates :funded_year, presence: true, numericality: […]

activerecord通过关联找到

我试图从我的数据库中检索一个activerecord对象。 我的模特是 class User :account end 和 class Account < ActiveRecord::Base has_many :domains has_many :users end 和 class Domain < ActiveRecord::Base belongs_to :account end 现在我想根据用户名和域名检索用户(假设这些是User和Domain类的属性)。 也就是说 User.find(:first, :conditions =>{:username => “Paul”, :domains => { :name => “pauls-domain”}}) 我知道上面的代码不起作用,因为我必须提到有关域表的一些内容。 此外,用户和域之间的关联是一对多的(这可能使事情进一步复杂化)。 关于如何形成这个查询的任何想法?

如何通过连接表填充has_many中的字段

我有一个关于活动记录关联的问题,请参考rails文档的这一部分: http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association 如果我们有三个型号: class Physician :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end class Patient :appointments end 文档说可以通过api以这种方式管理连接模型的集合: physician.patients = patients 但是,如果约会模型(如链接示例中)有一个名为appointment_date的字段,并且我想在特定日期为医生和患者创建新约会,该怎么办? 以下代码将在约会表中创建一条记录,但是如何在第三步中填充appointment_date呢? physician = Physician.first patient = Patients.first physician.patients << patient 做这样的事情存在吗? physician.patients.create( :patient => patient, ‘appointment.appointment_time’ => appointment_time )

如何使用has_many:through和honor:conditions创建新记录?

假设我有一个课程,学生可以通过会员注册(例如课程和学生的has_and_belongs_to_many关系)。 有些会员资格是针对刚刚观看课程的学生(不是为了学分等),所以: class Course :memberships has_many :observers, :through => :memberships, :source => :student, :conditions => { :memberships => { :observer => true }} end 这是有用的: observers = Course.find(37).observers 这是不起作用的: new_observer = Course.find(37).observers.build(:name => ‘Joe Student’) 我原本以为可以使用该关联构建新记录,这会产生: 新学生记录(’Joe Student’) 新的会员记录(course_id = 37,student_id =(joe),observer = true) 但相反,我得到: ActiveRecord::AssociationTypeMismatch: Membership expected, got Array 我敢肯定,我对这是多么的困惑,并会欣赏任何见解! 我也尝试在Membership模型上使用命名作用域执行此操作,但我似乎无法使用has_many在关联中使用作用域。 非常感谢您提供任何帮助!

Activeadmin formtastic动态选择

我想通过Activeadmin的formtastic做一个动态选择选项,如下所示: form do |f| f.inputs “Exam Registration Details” do f.input :user_id, :as => :select, :collection => User.where(:admin => ‘false’) #selects user from list. WORKING f.input :student_id, :as => :select, :collection => Student.joins(lessons: :user) #collection of students will change to students who have lessons with chosen user. NOT WORKING, returns all students who have lessons. f.input […]

Rails 3.2 has_many通过表单提交

我有一个has_many:通过表单,我无法获得额外的属性发布到数据库。 我在某个地方搞砸了参数名称。 我可以获取外键发布,但我有另一个属性,我试图在连接表中跟踪。 请记住,这是一个100%基于ajax的forms。 这就是我所知道的 编辑:在研究类似的问题后,我理解我应该构建表单属性,但我发现的代码由于某种原因不起作用。 这是一些资源。 http://railsforum.com/viewtopic.php?id=20203 Rails 3,嵌套的多级表单和has_many通过 我不明白的是,附加product_ids是内置到rails中的。 那些价值观。 为什么将quantity_shipped属性附加到该数组很难? Models and Relationships Shipment has_many :products :through => :product_shipments Product has_many :shipments :through => :product_shipments ProductShipments belongs_to :shipment, belongs_to :product ProductShipments table t.integer :shipment_id t.integer :product_id t.integer :qty_shipped <– This is the Problem Child 此部分循环显示几次显示来自某个供应商的所有产品。 它为product_ids生成一个数组,为product_shipments数量生成另一个数组。 _product_shipments.html.erb。 Assign Products to Ship product.id […]

Rails has_many通过表单与连接模型中的复选框和额外字段

我正试图解决一个非常常见的(我认为)任务。 有三种型号: class Product :categorizations accepts_nested_attributes_for :categorizations end class Categorization < ActiveRecord::Base belongs_to :product belongs_to :category validates :description, presence: true # note the additional field here end class Category < ActiveRecord::Base validates :name, presence: true end 我的问题始于产品新/编辑表格。 在创建产品时,我需要检查它所属的类别(通过复选框)。 我知道可以通过创建名称为’product [category_ids] []’的复选框来完成。 但是我还需要输入每个已检查关系的描述,这些关系将存储在连接模型(Categorization)中。 我在复杂的表格,habtm复选框等上看到了那些漂亮的Railscast。我一直在寻找StackOverflow。 但我没有成功。 我发现一篇文章描述了与我几乎完全相同的问题。 最后的答案对我来说是有道理的(看起来这是正确的方法)。 但它实际上并不是很好(即如果validation失败)。 我希望类别总是以相同的顺序显示(在新的/编辑表单中;在validation之前/之后)和复选框,以便在validation失败时保持原样等等。 任何人都赞赏。 我是Rails的新手(从CakePHP切换)所以请耐心等待,尽可能详细地写。 请指出我正确的方式! 谢谢。 🙂

无法通过关联更新has_many中的连接模型额外属性 – Rails

我仍然是Rails的学习者,正在制作一个Google Adwords模拟游戏,用于培训。 在游戏内部(我可能只解释这个问题的相关部分),我有三个模型,Adgroup(在我的代码中称为KeywordAdgroup), Keyword,AdgroupKeyword ,以及其中包含game_id的游戏模型。 这三种模式之间的关系是一个很好的结果has_many through: assciation,Adgroup通过AdgroupKeyword有很多关键字,而Keyword通过AdgroupKeyword有很多Adgroups。 现在,我想采用一种逻辑,即每个关键字只能添加到特定游戏的单个广告组中。 所以我在连接模型中添加了额外的属性game_id – AdgroupKeyword,并在此模型中采用validates_uniqueness_of :keyword_id, scope: :game_id 。 但是,我发现使用类似的代码,我可以使用.save方法validationCreate中的唯一性,但我无法使用.update(params)方法validationUpdate中的唯一性。 主要问题是,使用create和update的类似代码,create one可以save game_id save到连接模型(AdgroupKeyword),其中update(params)没有将game_id保存到连接模型,在控制台中,我可以看到update没有将game_id插入记录中。 它会变成像game_id: nil : # 但是save方法确实插入了game_id,当使用save方法插入时,game_id不是nil。 就像是 # 以下是我的代码: keyword_adgroup.rb (广告组的模型) class KeywordAdgroup < ApplicationRecord belongs_to :keyword_campaign has_many :adgroup_keywords, inverse_of: :keyword_adgroup, dependent: :destroy has_many :keywords, through: :adgroup_keywords, source: :keyword accepts_nested_attributes_for :adgroup_keywords, allow_destroy: true … accepts_nested_attributes_for […]