Tag: 多对多

多对多用户和组,但组拥有所有者

我无法理解/包围我的大脑。 我正在尝试创建一个允许这样的关系: 用户可以属于多个组 组可以有很多用户 一个组拥有一个用户所有者 集团所有权可以转让 我已经设置了多对多关系,但我似乎无法理解如何设置所有权function。 这是我目前在我的模型中所拥有的: class Group < ActiveRecord::Base has_and_belongs_to_many :users attr_accessible :name, :description, :isPublic, :tag_list, :owner end class User < ActiveRecord::Base has_and_belongs_to_many :groups attr_accessible :name, :description, :owner_id end 任何帮助将不胜感激!!

如何使用rails保护质量分配中的关联保存到数据库

尝试几个小时后,我无法保存到数据库。 上下文是这样的:我有两种类型的用户,一种用于我只需要非常基本的信息[用户名,电子邮件,密码]和另一种用户,我需要大量的信息[年龄,性别,城市等等] ] 我没有使用STI,因为表中会有大量的Null值。 所以我创建了这三种模式,其中用户具有简档(简档表)或不具有取决于其类型[1或2],并且该简档的字段是该用户所居住的城市,其与另一个表相关。数据库,城市表 class User < ActiveRecord::Base has_one :profile has_one :city, through: :profile end class Profile < ActiveRecord::Base belongs_to :user belongs_to :city […a bunch of fields here] end class City < ActiveRecord::Base has_many :profiles has_many :users, through: :profiles end 当我在rails控制台中玩它们时一切顺利: usr = User.new(name: “roxy”, email: “roxy@example.me”, password: “roxanna”, password_confirmation: “roxanna”, utype: 1) cty […]

如何使用:inverse_of与多个关联?

有谁知道我应该在下面的空白区域放置什么来使这种关系开箱即用? 我非常接近于完成这项工作,因为所有协会都能完美地工作。 唯一的问题是我无法保存附加水果的用户,因为目前没有:inverse_of。 我需要:inverse_of指向正确的方向,以便我可以用水果保存用户,而不必先保存用户,然后再将水果附加到它。 谢谢! 评论后更新: 用户模型: class User { bought }, class_name: ‘FruitsUser’, inverse_of: :buyer has_many :bought_fruits, through: :bought_fruits_users, class_name: ‘Fruit’, source: :bought_fruit has_many :sold_fruits_users, -> { sold }, class_name: ‘FruitsUser’, inverse_of: :seller has_many :sold_fruits, through: :sold_fruits_users, class_name: ‘Fruit’, source: :sold_fruit end 中台模型: class FruitsUser { where(type_of: ‘bought’) } scope :sold, -> { where(type_of: […]

使用嵌套属性在rails中的多对多关系的下拉菜单

我通过多对多关联有三个表:超市,产品和供应。 每个超市都可以容纳许多产品,每个产品都可以在许多超市销售。 该关联是通过Supply-model构建的。 超级市场: class Supermarket :supplies accepts_nested_attributes_for :products end 产品: class Product :supplies accepts_nested_attributes_for :supermarkets end 通过供应协会: class Supply < ActiveRecord::Base attr_accessible :supermarket_id, :product_id belongs_to :supermarket belongs_to :product end 我创建了脚手架,并填充了超市桌。 在我的产品表单中,我想使用一个(或多个)下拉菜单来选择相应的超市名称。 目标是创建一个新产品,同时通过Supply-table创建关联。 如果我想从那里选择相应的超市,那么代码在产品的forms和/或控制器中应该是什么样的?