Tag: 有很多通道

在has_many中使用集合<< object function:通过关系时,如何获取中间对象?

在定义了has_many:through关系后, @user = User.New(:name=>”Bob”) @project = Project.New( :name=>”Market Survey”) @user.projects << @project 有没有一种简单的方法来获取它创建的新中间对象? 例如在上面的例子中,如果中间表是“成员资格”,那么我可以使用: @membership = @user.projects << @project 我有这种感觉,必须有一种比我们一直做的更好的方法,即 @membership = Membership.where(:user_id=>x , :project_id=>y).first

Rails:构建关系之间的差异

doc有很多articles , 可以有很多edits 。 我想为每篇article构建一个edit ,直到@doc.articles的总数。 此代码适用于第一个构建(即,当尚未edits时)。 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 但是当edits已经存在时,它将保留这些edits并仍然为@doc.articles总计构建,如果只有一篇article被更改,最终@doc.articles太多的edits和一些重复。 我想提出一些条件:article_id ,它存在于edits和articles中(以伪代码forms ): unbuilt = @doc.articles – @doc.edits unbuilt.where(‘article_id not in (?)’, @doc.edits).reverse.each do |article| @doc.edits.build(…) end 任何帮助都会很棒! 非常感谢。

find_or_create on a有很多关系

我的应用程序中有很多关系: 节目有很多乐队通过=> 阵容 乐队是独一无二的:名字 class Show < ActiveRecord::Base attr_accessible :city_id, :title, :dateonly, :timeonly, :image, :canceled, :venue_attributes, :bands_attributes belongs_to :city belongs_to :venue has_many :lineups has_many :bands, through: :lineups has_and_belongs_to_many :users end class Lineup < ActiveRecord::Base belongs_to :show belongs_to :band end class Band < ActiveRecord::Base attr_accessible :name, :website, :country, :state has_many :lineups has_many :shows, through: :lineups validates […]

has_many通过中间模型不创建,但创建一个模型的副本

我有两个模型由中间模型连接 class Integration < ActiveRecord::Base has_many :integration_records has_many :records, through: :integration_records end class IntegrationRecord < ActiveRecord::Base belongs_to :integration belongs_to :record end class Record (0.1ms) BEGIN SQL (8.7ms) INSERT INTO “records” (“created_at”, “updated_at”) VALUES ($1, $2) RETURNING “id” [[“created_at”, Sat, 03 May 2014 00:31:02 UTC +00:00], [“updated_at”, Sat, 03 May 2014 00:31:02 UTC +00:00]] SQL (0.6ms) […]

使用has_many:through创建连接记录

好吧我以为我很好地遵循了这个答案…… 如何将记录添加到has_many:通过rails中的关联 。 但显然不是。 型号代码: 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 我基本上试图将库存与交易相匹配(有东西的人,有想要那些东西的人) 这是我想要实现的流程: 用户在哈希中提供数据,其中key =他们想要的itemlist_id ,值=他们想要的itemlist_id的quantity 。 假设用户想要9中的两个,看起来像这样: { 9 => 2} 对于用户提供的散列中的每个itemlist_id ,我将查看Inventories table并拉出inventory_ids ,其中itemlist_id与用户正在查找的itemlist_id相匹配,而inventory_id的所有者不是他或她自己的用户。 让我们说在Inventories table […]

如何将属性保存到has_many:通过连接表,没有要构建的现有记录

我有一个表单,使用accepts_nested_attributes_for创建一个新的子记录和一个新的父记录 孩子和父母有一个has_many :through协会: 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 Relationship < ActiveRecord::Base belongs_to :child belongs_to :parent accepts_nested_attributes_for :parents #has :schedule attribute end 我想同时在Relationships中设置schedule属性,并且不能使用accepts_nested_attributes_for因为我正在操作控制器中的某些数据以在保存之前确定schedule对象。 堆栈溢出有很多答案可以通过预先存在的父记录完成此操作,但是我找不到任何先前没有关联的情况。 我试图在模型中使用after_create回调来执行此操作,但是无法访问params并且已经读过这样做是不好的程序。 ( Rails如何将params从控制器传递到模型内的after_save )。 理想情况下,我想在相同的数据库调用中设置schedule ,以创建关系记录以及child_id和parent_id 。 对此有任何帮助都很棒,谢谢 […]

has_many:通过未初始化的常量

我已经阅读了关于has_many的文档和大量的教程:通过Rails中的关系,但我不能为我的生活搞定它。 我正在尝试将一个组添加到我的current_user(设计)中,并且我在Group和User之间有一个表具有状态(该组的用户状态可以更改)。 每当我创建一个新组时,我会收到一条错误,指出uninitialized constant Group::GroupUser 这是我的模特: groupuser.rb class GroupUser < ActiveRecord::Base belongs_to :group belongs_to :user end group.rb class Group :group_users accepts_nested_attributes_for :clients validates_length_of :name, :minimum => 5 validates_presence_of :name validates_presence_of :background validates_presence_of :clocktype end User.rb class User :group_users has_attached_file :avatar, :styles => { :medium => “300×300#”, :thumb => “100×100#” } validates_attachment_content_type :avatar, :content_type => [‘image/jpg’, […]

has_many通过其他属性

我们如何通过关联在has_many中设置其他参数? 谢谢。 Neelesh