Tag: 外键

has_one有两个外键?

我有两个类Message和User。 邮件具有sender_id和recipient_id两个用户的外键。 如何构建关系,我将能够为发件人和收件人获取用户,例如@ message.sender.name和@ message.recipient.name 我试着这样做: class Message ‘User’, :foreign_key => ‘sender’ belongs_to :recipient, :class_name => ‘User’, :foreign_key => ‘recipient’ end class User ‘Message’, :foreign_key => ‘recipient’ has_many :send_messages, :class_name => ‘Message’, :foreign_key => ‘sender’ end 但它没有帮助,当我试图访问,例如,@ message.recipient.name它说“未定义的方法`名称’”

Rails在两个表上自定义foreign_key名称

我有两个模型,例如User和Club及其属性: User: id uid email etc. 和 Club: id player_id address supporter etc. 出于某种原因,join属性是带有users.uid clubs.player_id , users.uid不是带有users.id 。 是否可以使用has_one和belongs_to将这两个模型与one-to-one关联连接起来? 谢谢

什么在Ruby on Rails 3中创建了FOREIGN KEY约束?

我知道默认情况下会创建id字段,还需要: PRIMARY KEY ( id ) 。 外键怎么样? 我有Shops和Products表以及以下协会: Shop: has_many :products Product: belongs_to :shop 在Product我还有: t.integer “shop_id” 这意味着是外键,还有: add_index(“products”, “shop_id”) 但是,如果我导出数据库,我只看到: KEY `index_products_on_shop_id` (`shop_id`) 我该怎么做才能添加 FOREIGN KEY (`shop_id`) REFERENCES Shop(`id`) ?

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 = […]

使用其他模型的值下拉菜单

我有属于collections品的产品。 集合只是一个名称。 产品有collection_id。 在我用于创建和编辑产品的_form视图中,我想要一个带有所有集合名称的下拉菜单。 问题,似乎没有附加到form.for的选择方法,我试图使用: select(method,choices,options = {},html_options = {}) 从文档但我不明白。 我必须写一个方法来创建一个表单? 有哪些选择,有两种选择? 两个参数应足以填充标记。 我怎么能有一个下拉菜单,让我通过集合名称为我的产品分配一个集合?

支持Rails中的外键约束

在Ruby on Rails中,如何在迁移中添加外键约束?

从Rails中的子对象访问父对象属性

我有一个名为Category的模型,如下所示: class Category “parent_id” end 我有一个视图,显示所有类别及其一些属性。 我可以访问category.parent_id ,但我希望能够做类似category.parent_name事情。 我可以看到自己创建一个模型方法来获取所有类别并使用每个类别的对应父名称填充集合,但我想知道是否有任何方式可以轻松地执行此操作。 编辑:我修改了模型,让它像这样: class Category ‘Category’, :foreign_key => ‘parent_id’ belongs_to :parent, :class_name => ‘Category’, :foreign_key => ‘parent_id’ end 创建表类别的迁移如下所示: class CreateCategories < ActiveRecord::Migration def change create_table :categories do |t| t.string :name t.text :description t.integer :parent_id t.timestamps end end end 但是,当我将类别对象传递给视图时,我无法通过执行category.parent.name来访问其父属性 – 对该对象进行inspect可以让我: 如果我检查category.parent我得到这个: # 但是,如果我尝试执行category.parent.name则会收到以下错误: undefined method `name’ […]

Rails是否需要数据库级约束?

我遇到与以下post相同的问题。 所以我想知道,为什么Rails默认不支持生成外键? 不是必要的吗? 或者我们应该手动完成吗?

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” […]

如何在Rails 4中设置自定义字符串外键?

如何使用string foreign_key设置关联以正确设置has_one? class Pharmaceutic < ActiveRecord::Base has_one :pharmaceutic_group, foreign_key: "code" end class PharmaceuticGroup > Pharmaceutic.last.pharmaceutic_group Pharmaceutic Load (0.3ms) SELECT `pharmaceutics`.* FROM `pharmaceutics` ORDER BY `pharmaceutics`.`id` DESC LIMIT 1 PharmaceuticGroup Load (0.3ms) SELECT `pharmaceutic_groups`.* FROM `pharmaceutic_groups` WHERE `pharmaceutic_groups`.`code` = 2 ORDER BY `pharmaceutic_groups`.`id` ASC LIMIT 1 => nil >> Pharmaceutic => Pharmaceutic(id: integer, package_type: integer, group_code: […]