Tag: 有很多通过

Rails关联:如何限制/范围has_many:通过多个自引用条件?

最好的方法是什么? 我正在尝试这样的事情,但感觉……错了。 (注意:所有关于:has_many关联中的条件……) class Submission :checklist, :source => :job, :conditions => [“jobs.archived = false OR jobs.archived_at > #{self.created_at} OR jobs.created_at < #{self.created_at}"] end Rails 3.2.11,Ruby 1.9.3 我想做什么 我需要在has_many上传递多个条件:通过关联并引用“自我”模型来在这些条件下进行比较。 我找到了多个条件的例子。 我找到了自我参照比较的例子。 但我找不到这两个元素的任何东西。 这让我觉得我做错了。 背景和定义 提交在某个时间点记录清单及其关联作业的快照。 因此,我希望能够说“submission.jobs”并查看在提交本身创建时与提交的核对表关联的作业。* *它实际上稍微复杂一点:我想要在创建提交时存在的作业以及当时尚未存档的作业。 清单是动态的,可以随时间变化,但我总是通过自行建立的归档系统保持历史记录的完整性,因此当为该清单创建提交时,清单上的作业将是Checklist的Job,其中Job.created_at之前是Submission.created_at(尚未存档)。 因此,虽然提交属于清单,而清单属于清单,但是对于特定提交而言,唯一重要的工作是在创建提交时存在(并且尚未存档)的作业。

有很多通过两个模型

我有两个重要的模型。 还有更多的模型与它们相互作用,但只有这些模型应该是相关的。 class Image < ActiveRecord::Base belongs_to :user belongs_to :trip end class Mission < ActiveRecord::Base belongs_to :user belongs_to :trip # has_many :images, through: :user end 注释掉的线路让我在那里中途,但我希望满足图像的附加条件,其具有与任务相同的行程ID。 # has_many :images, through: [:user, :trip] 使用类似于某些类似方法的数组有时会采用无效语法。 def images Image.where(user_id: user_id, trip_id: trip_id) end 我应该这样做吗? 或者有更好的方法吗? 我也试过了has_many上的条件,但是我放在那里的任何动态都被调用了Image::ActiveRecord_Relation 以下是一些示例记录: 所以Mission.find(1).images给出了3个图像,而2和3给出了一个空数组。

为rails 3 has_many创建的错误数据库记录:通过关联

我有一个has_many:通过关联。 球员有很多球队,球队有很多球员。 联盟模式Affiliation属于玩家和团队,并且还具有year属性,用于跟踪玩家year的团队归属(或就业)。 我似乎无法找到基于以下规则构建关联的正确方法: 创建一个新玩家。 关联可能是新的或现有的团队。 所以找到它或创建它,但只有在保存播放器时才创建它。 该关联可能包括也可能不包括一年,但只有在保存玩家和团队时才能创建该关联。 播放器模型如下所示: class Player :destroy has_many :teams, :through => :affiliations end 团队模型如下所示: class Team :destroy has_many :players, :through => :affiliations end Affiliation模型如下: class Affiliation < ActiveRecord::Base attr_accessible :player_id, :team_id, :year belongs_to :player belongs_to :team end 我已成功使用PlayersController中的create动作创建没有join模型属性的关联记录,如下所示: class PlayersController < ApplicationController def create @player = Player.new(params[:player].except(:teams)) unless params[:player][:teams].blank? params[:player][:teams].each […]

使用关联(has_many)模型中的字段和rails中的formtastic

我搜索并尝试了很多,但我无法按照我的意愿完成它。所以这是我的问题。 class Moving :destroy has_many :resources, :through => :movingresources end class Movingresource < ActiveRecord::Base belongs_to :moving belongs_to :resource end class Resource :movingresources end Movingresources包含其他字段,如quantity 。 我们正在研究“账单”的观点。 感谢formtastic通过写作简化整个关系的事情 :check_boxes %> 我得到一个真正漂亮的复选框列表。 但到目前为止我还没有发现的是:我如何使用’movingresource’中的附加字段,在每个复选框的下方或下方,从该模型中获取所需的字段? 我看到了不同的方法,主要是手动循环遍历一组对象并创建适当的表单,使用:for in form.inputs part,or。 但是这些解决方案都不是干净的(例如,为编辑视图工作但不适用于新的,因为没有构建或生成所需的对象并且生成它们导致混乱)。 我想知道你的解决方案!

ruby on rails has_many:通过关联,其中有两列具有相同的模型

我有以下型号: class UserShareTag “User” belongs_to :post belongs_to :sharee, :class_name => “User” validates :sharer_id, :presence => true validates :sharee_id, :presence => true validates :post_id, :presence => true end 在Post模型中,我有以下几行: has_many :user_share_tags, :dependent => :destroy has_many :user_sharers, :through => :user_share_tags, :uniq => true, :class_name => “User” has_many :user_sharees, :through => :user_share_tags, :uniq => true, :class_name => “User” […]

Rails:为什么“has_many …,:through => …”关联导致“NameError:uninitialized constant …”

为了表示一个组可以有多个用户,并且一个用户可以属于多个组,我设置了以下关联: class Group :users_groups end class User :users_groups end class UsersGroups < ActiveRecord::Base belongs_to :user belongs_to :group end 但是,当我键入: Group.find(1).users 我明白了: NameError: uninitialized constant Group::UsersGroup 我究竟做错了什么 ?

Rails has_many:通过collection_select表单帮助程序

我在创建一个可以保存我的has_many的表单时遇到问题:通过关联。 我通过发布json成功保存了,但这些表单对我来说还不行。 由表单提交创建的请求参数将无法解决。 任何指导我解决方案的帮助都会帮助我不再浪费时间。 先谢谢你。 编辑 – 添加了forms_for的尝试和创建的params json,它在底部不起作用 – Json发布请求有效的参数: { “author”: { “name”: “Author Name”, “post_authors_attributes”: [ {“post_id”:”1″}, {“post_id”:”2″}, {“post_id”:”3″} ] } } Rails形成了无法保存的参数。 { “author”: { “name”: “assd”, “post_authors_attributes”: [ “”, “2”, “3” ] } } ……以及相关的代码示例…… 作者模型 class Author :post_authors accepts_nested_attributes_for :post_authors end 发布模型 ( 目前只在作者上工作有很多post,而不是相反 ) class Post < ActiveRecord::Base […]

has_many:通过外键?

我已经阅读了多个有关此问题的问题,但尚未找到适合我情况的答案。 我有3个型号: Apps , AppsGenres和Genres 以下是每个相关领域: Apps application_id AppsGenres genre_id application_id Genres genre_id 这里的关键是我没有使用那些模型中的id字段。 我需要根据那些application_id和genre_id字段关联表。 这是我目前得到的,但它没有得到我需要的查询: class Genre :application_id, :foreign_key => :application_id has_many :apps, :through => :apps_genres end class AppsGenre :application_id belongs_to :genre, :foreign_key => :application_id, :primary_key => :application_id end class App :application_id, :primary_key => :application_id has_many :genres, :through => :apps_genres end 作为参考,这是我最终需要的查询: @apps = […]

many-to-many: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 我试图了解如何做以下两件事: 如何设置创建新患者的视图,并为具有指定时间的现有医生分配约会 如何为现有患者分配新医师和预约时间 我经历了处理嵌套表单的RailsCasts 196和197,但我不知道它将如何应用于这种情况。 有人可以提供一个例子或指向我这方面的指南吗? 谢谢

如何通过关联在has_many中使用回调?

我有一个通过has_many通过项目模型关联的任务模型,需要在通过关联删除/插入之前操作数据。 由于“ 自动删除连接模型是直接的,因此不会触发销毁回调。 ”我无法使用回调。 在任务中,我需要所有project_ids在保存任务后计算项目的值。 如何通过关联禁用删除或更改删除以销毁has_many? 这个问题的最佳做法是什么? class Task has_many :project_tasks has_many :projects, :through => :project_tasks class ProjectTask belongs_to :project belongs_to :task class Project has_many :project_tasks has_many :tasks, :through => :project_tasks