Tag: association

用户团队应用程序

我需要创建用户团队。 用户属于团队(仅一个团队),团队拥有许多用户。 我无法弄清楚如何让用户能够创建,加入和离开团队。 以下是我到目前为止所做的事情,但我确信我做得非常糟糕(并且“newby”错误)。 用户模型: belongs_to :teams, dependent: :destroy def team_member?(team) team_relationships.find_by_team_id(team.id) end def join!(team) team_relationships.create!(team_id: team.id) end def unjoin!(team) team_relationships.find_by_team_id(team.id).destroy end 团队模型 has_many :users, through: :team_relationships, dependent: :destroy attr_accessible :team_name, :team_id validates :user_id, presence: true validates :team_name, presence: true, length: { maximum: 140 } default_scope order: ‘teams.created_at DESC’ team_relationship模型 attr_accessible :team_id belongs_to :team belongs_to […]

如何使用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在关联中使用作用域。 非常感谢您提供任何帮助!

如何使用复选框应用带有acts_as_taggable_on的标签?

我想使用acts_as_taggable_on为公司模型分配两种不同的“类型”标签(扇区类别和免费标记)。 NB:我是RoR的新手! 如果只使用标准文本输入字段,这很容易做,但我想在一种类型(预定义的固定扇区类别标记)上使用复选框,然后允许用户在输入字段中添加逗号分隔标记。 我已经以各种方式解决了这个问题,……一个受这个问题启发的问题 ……但我无法让它发挥作用 这是我到目前为止: # models/company.rb class Company … acts_as_taggable_on :tags, :sectors has_many :taggings, :as => :taggable, :include => :tag, :class_name => “ActsAsTaggableOn::Tagging”, :conditions => { :taggable_type => “Company” } has_many :sector_tags, :through => :taggings, :source => :tag, :class_name => “ActsAsTaggableOn::Tag”, :conditions => {:context => “sectors”} end 在表单中(使用simple_form gem)我有…… # views/companies/_form.html.haml = simple_form_for […]

rails mongoid标准通过关联找到

我正在尝试通过关联的用户名来查找记录,该用户名包含在belongs_to关系中,但它不起作用。 文章属于用户用户有很多文章 Article.where(user_id: someid)工作正常,但我想使用用户名作为参考,存储在Users表中。 Article.includes(:user).where(:username => “erebus”) Article.includes(:user).where(“user.username” => “erebus”) 我也有identity_map_enabled: true Article.includes(:user).inclusions返回关系详细信息 不起作用,我不理解什么?

Rails模型,属于很多

我很难弄清楚如何将我的一个模型与另一个模型联系起来。 就像现在一样,我有: class ModelA < ActiveRecord::Base has_many :model_b end class ModelB < ActiveRecord::Base belongs_to :model_a end 但是…… ModelB不仅需要属于ModelA的一个实例,而且可能属于三个。 我知道有一个has_many:through,但我不确定它在这种情况下会如何起作用。 ModelA的每个实例总是会有三个ModelB实例。 但如前所述,ModelB不仅仅属于ModelA的一个实例。

Rails habtm并找到没有关联的记录

我有2个型号: class User < ActiveRecord::Base has_and_belongs_to_many :groups end class Group < ActiveRecord::Base has_and_belongs_to_many :users end 我想创建一个范围 (这对于效率和链接范围的能力很重要),它返回不属于任何组的用户。 经过多次尝试,我没有做一个方法而不是范围,这使得在User.all上collect丑陋而且……不对。 有帮助吗? 也许对于第二个问题:我设法创建一个范围,返回属于任何给定组的用户(以id的数组给出)。 scope :in_groups, lambda { |g| { :joins => :groups, :conditions => {:groups => {:id => g}}, :select => “DISTINCT `users`.*” # kill duplicates } } 可以更好/更漂亮吗? (使用Rails 3.0.9)

ActiveRecord找到所有有关联孩子的父母

我不知道为什么我无法解决这个问题,我认为应该相当简单。 我有两个型号(见下文)。 我正在尝试为SupplierCategory提出一个命名范围,该范围会找到所有SupplierCategory(包括:供应商),这些供应商的关联供应商不是空的。 我尝试了一个直接加入, named_scope :with_suppliers, :joins => :suppliers商,它只给我供应商的类别,但它给了我每个类别单独列出,所以如果一个类别有2个供应商,我在返回时获得两次类别数组: 目前我正在使用: named_scope :with_suppliers, :include => :suppliers 然后在我看来我正在使用: ‘category’, :collection => @categories.find_all{|c| !c.suppliers.empty? } %> 不完全雄辩,但说明了我想要实现的目标。 类定义 class SupplierCategory “name” end class Supplier < AR belongs_to :supplier end

Rails Association已经有了最新记录

我有以下型号: class Section ‘SectionRevision’, :foreign_key => ‘section_id’ has_many :references has_many :revisions, :class_name => ‘SectionRevision’, :foreign_key => ‘section_id’ delegate :position, to: :current_revision def current_revision self.revisions.order(‘created_at DESC’).first end end current_revision是最近创建的revision 。 是否可以将current_revision转换为关联,以便我可以像Section.where(“current_revision.parent_section_id = ‘1’”)那样执行查询? 或者我应该将current_revision列添加到我的数据库,而不是尝试虚拟或通过关联创建它?

ActiveRecord:inverse_of不适用于has_many:通过on上的连接模型

我无法在创建时使用inverse_of来处理连接模型。 我不确定这是一个错误还是没有实现。 我有以下型号: class Challenge :group_challenges has_many :group_challenges, :inverse_of => :challenge attr_accessor :contact_ids end class GroupChallenge :group_challenges belongs_to :group, :inverse_of => :group_challenges before_save :check_challenge def check_challenge Rails.logger.debug(“challenge.contact_ids: #{challenge.contact_ids}”) end end class Group :group_challenges has_many :group_challenges, :inverse_of => :group end contact_ids是我的challenge的虚拟属性,我想在创建该关联时在group_challenges模型中访问这些属性。 我无法让它发挥作用。 这是一个例子: challenge = Challenge.new :groups => Group.all, :contact_ids => [1,2,3] # log output => […]

ActiveRecord:如何克隆嵌套关联?

我目前正在克隆这样的单级关联: class Survey < ActiveRecord::Base def duplicate new_template = self.clone new_template.questions << self.questions.collect { |question| question.clone } new_template.save end end 因此, Survey克隆然后克隆与该调查相关的Questions 。 精细。 这非常有效。 但我遇到的问题是每个问题都有很多问题。 所以Survey has_many Questions which has_many Answers 。 我无法弄清楚如何正确克隆答案。 我试过这个: def duplicate new_template = self.clone self.questions.each do |question| new_question = question.clone new_question.save question.answers.each do |answer| new_answer = answer.clone new_answer.save new_question.answers […]