Tag: 关系数据库

如何查找记录,其has_many通过对象包括某些列表的所有对象?

我有一个典型的标签和任何对象关系:说 class Tag :tagazations end class Tagazation { :scope => :project_id } end class Project :tagazations end 这里没什么特别的:每个项目都有一个或多个标签。 该应用程序具有搜索function:您可以选择某些标签,我的应用程序应该显示所有标记有所有提及标签的项目。 所以我得到了一个必要的tag_ids数组,然后遇到了这么简单的问题

Rails has_many通过has_many与多个模型

模拟以下情况的最佳方法是什么: Word belongs_to :wordable, :polymorphic => true Phrase has_many :words, :as => :workable belongs_to :story Line has_many :words, :as => :wordable belongs_to :story Story has_many :lines has_many :phrases has_many :words, :through => :phrases has_many :words, :through => :lines 我希望能够做到 @story.words 获取通过线条或短语链接到故事的所有单词的列表… 那可能吗?

Rails – 如何设置可属于3种不同模型的模型

我正在尝试制作一个测试应用程序,类似于您在学校的体验。 我有一个模型问题,可以属于考试,测验或作业。 我应该为“:exam_id,:integer,:null => false;:quiz_id,:integer,:null => false;:assignment_id,:integer,:null => false;”创建字段? 这个问题将属于其中一个或几个或全部(因此我可以在差异模型中重复使用相同的问题)。 我应该删除:null => false,以便它可以属于它们中的任何一个….或者设置它的最佳方法是什么?

如何建立典型的用户HABTM角色关系

我对此很新,我正在使用cancan + devise为我的用户身份validation。 但是,我不确定设置典型用户HABTM角色关系意味着什么,我也不了解HABTM关系是什么。 任何人都可以解释得很好或者指向一个好的教程或例子吗?

has_many:通过多个has_one关系?

我正在为我们的铁路教堂写一个导师计划(我仍然很喜欢铁路)。 我需要对此进行建模.. contact has_one :father, :class_name => “Contact” has_one :mother, :class_name => “Contact” has_many :children, :class_name => “Contact” has_many :siblings, :through , :source => :children 所以基本上一个对象“兄弟姐妹”需要映射父亲和母亲的所有孩子,不包括对象本身。 这可能吗? 谢谢 丹尼尔

Rails 5:从连接表中显示表单

这是我第一次涉及连接表和多对多关系,我对Ruby / Rails也相对较新。 这是对前一个问题的直接跟进,我在其中构建了相应的相关表格/模型。 但为了澄清,我将在这里重新定义布局…… 我的模特: order.rb class Order < ApplicationRecord belongs_to :user has_many :quantities has_many :meals, through: :quantities end meal.rb class Meal < ApplicationRecord has_many :quantities has_many :orders, through: :quantities end quantity.rb class Quantity < ApplicationRecord belongs_to :order belongs_to :meal accepts_nested_attributes_for :quantities end user.rb class User < ApplicationRecord # Include default devise modules. Others […]

DataMapper因关系而无法删除记录

我有许多使用Torrent和Tag的DataMapper / MySQL设置,如下所示: class Torrent include DataMapper::Resource property :id, Serial property :name, String property :magnet, Text property :created_at, DateTime has n, :tags, :through => Resource end class Tag include DataMapper::Resource property :id, Serial property :name, String property :hits, Integer has n, :torrents, :through => Resource end 但是,当试图通过Torrent.first.destroy或类似的东西来破坏一个torrent时,DataMapper会返回false 。 我尝试了直接的SQL查询,例如delete from torrents where name like ‘%ubuntu%’ […]

Rails:违反外键约束

我有三种模式: Book , genre , BookGenre ,这里有关系: class BookGenre < ActiveRecord::Base belongs_to :book belongs_to :genre end class Book < ActiveRecord::Base has_many :book_genres has_many :genres, through: :book_genres end class Genre < ActiveRecord::Base has_many :book_genres has_many :books, through: :book_genres end 然后我使用seed文件将数据放入这些表中。 但是当我想再次使用rake db:seed时,它显示了这个错误 ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table “books” violates foreign key constraint “fk_rails_4a117802d7” […]

Ruby on Rails:在单个数据库单元中保存多个值

如何在Ruby on Rails应用程序中的单个单元格记录中保存多个值? 如果我有一个名为Exp的表,其名称为: Education , Experience和Skill ,如果我希望用户在一行中存储多个值,例如:教育机构或技能,那么最佳做法是什么? 我想让用户使用多个文本字段,但应该进入相同的单元格记录。 例如,如果用户具有多种技能,那么这些技能应该在一个单元格中? 这会是最好的还是会更好,如果我创建一个新技能表? 请指教, 谢谢