Tag: 拥有并且属于许多人

如何动态链接rails中的where子句?

我有一个video模型和一个标签模型。 标签HABTMvideo和videoHABTM标签。 class Tag (id) { where(id: id) } end class Video < ApplicationRecord has_and_belongs_to_many :tags end 我想知道如何创建一个查询,其中未知数量的标签将来自用户。 所以我可以说,“告诉我所有标有(123和124和125)的video”。 最终的目标是我不知道用户将传递多少标签,因此我需要知道如何创建一系列范围并最终能够将整个事物传递到hansack以进行文本搜索结果video的一些字段。 我怎么能做到这一点? 更新:我想知道如何使用Rails和ActiveRecord执行此操作。 我知道如何用SQL实现这一目标。 但仅使用SQL完成它将不允许我将结果关系传递给ransack。

如何在Rails中两次加入相同的2个模型?

我有一个国家偏好的应用程序。 用户将有两种类型的国家偏好 – 事件和研究。 将来可能会有更多。 我更倾向于使用2个表来表示使用STI。 我在执行此操作时优雅地配置Rails有点麻烦。 我可以破解它,但我宁愿通过Rails惯例这样做。 我想要的是这样的: class User :event_countries, :class_name => ‘Country’ has_many research_countries, :through => :research_countries, :class_name => ‘Country’ end class EventCountry < ActiveRecord::Base belongs_to :country belongs_to :user end class ResearchCountry < ActiveRecord::Base belongs_to :country belongs_to :user end class Country < ActiveRecord::Base … end 但这不起作用。 鉴于这个“伪代码”有谁知道如何在Rails中实际实现这一点?

使用collection_select创建HABTM关系中的多个记录 – Rails

我在训练和小组之间有一个has_and_belongs_to_many关系。 我在群组中有一个collection_select ,用于向群组添加锻炼。 问题是我只能更改HABTM表中的一条记录,所以我只能添加一条记录,然后编辑该记录。 如何添加其他记录? 有任何想法吗? 这是一些代码: show.html.erb: “”} %> 。 class Workout < ActiveRecord::Base attr_accessible :name, :exercises_attributes, :workout_exercises_attributes, :group_ids has_and_belongs_to_many :groups 。 class Group < ActiveRecord::Base attr_accessible :cycle_id, :name, :next_group_id, :previous_group_id, :workout_ids has_and_belongs_to_many :workouts

habtm关系不支持:依赖选项

HABTM关系不支持:dependent选项是真的吗? class Person :destroy end 我正在尝试rails edge。

对称的,自我指涉的HABTM关系

在我的架构中,我有一个名为Imprintables的模型。 在这个模型中,我有这种自我指涉关系: class Imprintable < ActiveRecord::Base has_and_belongs_to_many :coordinates, class_name: 'Imprintable', association_foreign_key: 'coordinate_id', join_table: 'coordinates_imprintables' … end 在可打印的创建/编辑表单中,我有一个选择字段,用户可以在其中选择坐标imprintables(坐标用于标识与正在创建/编辑的imprintables类似的imprintables)。 我的问题是,如果我有一个可以打印的A和可打印的B,当我创建A并将列表B作为一个可以打印的坐标时,我希望能够查看B的编辑表单并查看A也列为坐标。 我已经尝试修改控制器的更新并创建操作以将两对id插入到链接器表(coordinates_imprintables)中。 例如,如果A.id = 1且B.id = 2,则执行一些sql,如: INSERT INTO coordinates_imprintables (imprintable_id, coordinate_id) VALUES (1, 2) INSERT INTO coordinates_imprintables (imprintable_id, coordinate_id) VALUES (2, 1) 但我无法让它工作,因为我不知道如何知道用户何时尝试删除或插入坐标,我甚至不确定这是否是正确的答案。 Railscasts略有帮助,这似乎是一个棘手的案例,因为我有一个具有自我指涉关系的模型也是对称的。 谢谢你的时间!

在Rails中has_and_belongs_to_many

在rails中使用has_and_belongs_to_many关联而不是has_many:through有什么明显错误吗? 我知道这些 文章 描述了差异和解决方法,但它们是从2006年开始的。从我读过的东西来看,似乎人们认为habtm陈旧而笨重,但如果一个简单的多对多加入没有必要的模型是你在找什么? 思考?

Rails:在HABTM关系中查找没有连接的行

我有两个模型, Users和Leads与HABTM关系: class Lead < ActiveRecord::Base has_and_belongs_to_many :users end class User < ActiveRecord::Base has_and_belongs_to_many :leads end 我现在怎样才能获得那些与用户无关的潜在客户? 提前致谢!

尝试使用accepts_nested_attributes_for和has_and_belongs_to_many,但未填充连接表

我正在学习RoR并尝试使用accepts_nested_attributes_for和has_and_belongs_to_many来提交传统上是两种forms的信息。 我在一些网站上看到它们兼容,有些网站不兼容,有些网站不知道。 作为参考,我使用的是Rails 2.3.4。 我尝试使用嵌套模型上的Ryan’s Scraps教程对我的解决方案进行建模 从我试图调试,似乎我有两个问题,但我不知道为什么。 当我提交带有嵌套模型的表单时,只会发布部分嵌套模型信息。 我只得到第一个字段,而不是用户可能选择的“n”个字段 在发布的单个字段中,没有为我为HABTM关系创建的连接表中插入任何行。 这是我的插入尝试的一段代码和相应的日志: 律师模型: class Attorney proc { |a| a[‘name’].blank? } end 协会模型: class Association “Please enter an association name.” end 律师控制员: def new @attorney = Attorney.new @attorney.associations.build respond_to do |format| format.html # new.html.erb format.xml { render :xml => @attorney } end end def create @attorney = […]