Tag: 多对多

如何在factory_girl中构建/创建多对多关联?

我有一个与Email模型有多对多关系的Person模型,我想创建一个工厂,让我为该人生成名字和姓氏(已经完成),并创建一个电子邮件地址,基于那个人的名字。 以下是我创建一个person姓名的方法: Factory.sequence :first_name do |n| first_name = %w[FirstName1 FirstName2] # … etc (I’m using a real subset of first names) first_name[(rand * first_name.length)] end Factory.sequence :last_name do |n| last_name = %w[LastName1 LastName2] # … etc (I’m using a real subset of last names) last_name[(rand * last_name.length)] end Factory.define :person do |p| #p.id ??? p.first_name […]

食谱模型中的成分总和创建购物清单

我有一个购物清单模型,有许多食谱,这也有很多成分。 class Shoppinglist :shoppinglistrecipezations end class Recipe :shoppinglistrecipezations has_many :ingredients, :through => :ingredienzations has_many :ingredienzations end class Ingredient :ingredienzations has_many :ingredienzations end 当我在购物模型中添加几个食谱(其中一些具有相同的成分)时,我想打印购物清单中所有配料的清单和适量的配料。 Shoppinglistingredienzations有一个整数“人”来告诉应该计算多少人服务,而食谱有一个人变量来显示食谱的人数。 Ingredienzations包含数量,测量类型(克,茶匙等)和recipe_id。

将关联从一个更改为多个到多个到多个

我想有两个模型 class A < ActiveRecord::Base has_many :bs end class B < ActiveRecord::Base belogns_to :a end 现在因为一些系统更改,我需要将这种关联转换为很多很多这样的事情 class A < ActiveRecord::Base has_and_belongs_to_many :bs end class B < ActiveRecord::Base has_and_belongs_to_many :as end 要么 class A < ActiveRecord::Base has_many :cs has_many :bs, through: :cs end class B < ActiveRecord::Base has_many :cs has_many :as, through: :cs end class C […]

RoR – 在多对多连接表上创建记录

我有以下型号: Client.rb has_many :establishments accepts_nested_attributes_for :establishments has_many :addressess, through: :establishments accepts_nested_attributes_for :addresses Establishment.rb belongs_to :address belongs_to :client Address.rb has_many :establishments 在Client的show视图中,我创建了一个 ,以便为客户端创建一个新的Address记录和一个新的Establishment。 在AddressController我有: def new @client = Client.find(params [:client_id]) @address = @ client.addresses.build 结束 def create @address = Address.new(address_params) respond_to do |format| if @address.save format.html { redirect_to @address, notice: ‘Estabelecimento criado com sucesso.’ } format.json […]

如何使用多对多关系post#show在ruby on rails

在团队#model中 class Team :postteams end 在Post#model中 class Post :postteams accepts_nested_attributes_for :postteams end 在Postteam模型中 class Postteam < ActiveRecord::Base belongs_to :post belongs_to :team end 通过这些关系,我终于成功发布了这些多个团队,并将这些团队保存在数据库中,就像这样 1 这是我的post#controller : class PostsController team) end end respond_to do |format| if @post.save format.html { redirect_to @post, notice: ‘post was successfully created.’ } format.json { render :show, status: :created, location: @post } […]

has_many中的视图:通过关系

我一直在修补这个问题已有一段时间了,似乎无法弄明白这一点。 它可能很简单,但是这里有: 我在’层压’和’标准’之间有一个has_many:trough关系,加上模型’标准化’。 Standard.rb class Standard :standardizations end Standardization.rb class Standardization < ActiveRecord::Base attr_accessible :laminate_id, :standard_id belongs_to :laminate belongs_to :standard end Laminate.rb class Laminate :standardizations end 方案是层压板可以属于几个标准,我已经在视图的新部分工作 – 复选框和一切。 我的问题是在尝试显示给定层压板的相应标准的名称时。 到目前为止,我能够显示层压板所分配的标准,但不仅仅是标准的名称。 我的show.html.erb说: 这会使一切正确,但是说 ……不起作用。 我怎样才能利用每个人的名字,分配标准? Laminate_controller: class LaminatesController < ApplicationController # GET /laminates # GET /laminates.json def index @laminates = Laminate.all @standards = Standard.all respond_to […]

Rails 3范围 – 查找不在特定组中的用户

这是我的问题(有点简化): 我有以下型号: class User has_many :group_users has_many :groups, through: :group_users class Group has_many :group_users has_many :users, through: :group_users class GroupUser belongs_to :group belongs_to :user scope :belonging_to_group ->(group) {where(group_id = group.id)} 我想对不在一个特定组中的用户进行范围,让我们说蔬菜的例子,这样的事情会是这样的: scope :not_in_group, ->(group) 我尝试过这样的条款: scope :not_in_group, ->(group) {joins(:group_users).merge(::GroupUser.belonging_to_group(group)).group(:title).having(‘count(group_users.user_id) = 0’) 但似乎没什么用 编辑 :我现在有另一个问题,你可能想要检查这个,如果你从另一个类的类方法调用你的范围: Rails – 使用另一个类方法的类方法与第二类的一个属性

多对多关系只能以Rails / Activerecord / devise的方式工作

我有一个多对多的关系用户到列表用户模型是用设计创建的。 出于某种原因,我无法访问任何用户的.lists。 为nil返回undefined方法`to_sym’:NilClass。 在做了一些挖掘之后我很确定我已经在_reflect_on_associationactiverecord(4.1.6)lib / active_record / reflection.rb中找到了问题 def _reflect_on_association(association) #:nodoc: _reflections[association.to_sym] end 协会正在以零传递。 有没有人遇到过这个问题? 在rails控制台中测试关系 [15] pry(main)> testuser = User.new => # [16] pry(main)> testlist = List.new => # [17] pry(main)> testlist.users => [] [18] pry(main)> testuser.lists NoMethodError: undefined method `to_sym’ for nil:NilClass from /Users/user/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/reflection.rb:100:in `_reflect_on_association’ [19] pry(main)> testlist.users < [#] [22] pry(main)> testuser.lists […]

Rails为具有多对多关系的模型创建表单

我有两个模型, Recipe和Tag ,具有has_and_belongs_to_many关系。 对于这种关系,我有一个简单的连接表, RecipesTags 。 食谱: has_and_belongs_to_many :tags 标签: has_and_belongs_to_many :recipes 现在,在创建新配方时,用户可以以复选框的forms填写配方所属的类别,例如“肉”,“鱼”等。 这些类别实际上只是数据库中的标签。 问题:食谱没有保存任何标签。 配方new并create控制器方法: def new @recipe = Recipe.new @ingredients = Ingredient.all @tags = Tag.all respond_to do |format| format.html # new.html.erb format.json { render json: @recipe } end end # POST /recipes # POST /recipes.json def create @recipe = Recipe.new(params[:recipe]) if (params[:tags]) @recipe.tags […]

订购与第二模型相关的第一模型

我正在研究我的第一个项目与RoR,我需要在两个模型之间创建多对多的关系,但有可能将第一个模型的对象与第二个模型相关联。 假设我有两个以下型号 – 客户 – 路线 我想为许多路由分配许多客户,但是例如存储该协会的存储顺序 -Route 1 –Customer 2, position 1 –Customer 1, position 2 -Route 2 –Customer 3, position 1 –Customer 1, position 2 我想我必须使用has_many:through和belongs_to并在中间表中创建“position”字段,但是如何使这个字段可访问和编辑?