`
sp42
  • 浏览: 147334 次
  • 来自: Canton
文章分类
社区版块
存档分类
最新评论

belongs_to vs HasMany

阅读更多

Ruby on Rails belongs_to

belongs_to complements a has_many or has_one association.

In general, the Foo model belongs_to :bar if the foo table has a bar_id foreign key column.

Examples of this:

  • Companies database, where an account belongs to a company.
  • Forum system, where a topic belongs to a forum, which belongs to a category.
  • a gallery system where a thumbnail belongs to a picture.

Example Forum

See ForumExample.

Notes

When using :counter_cache => true, be sure the default value on your model_count column defaults to something that can be incremented. NULL + 1, for instance, equals NULL so the counter SQL won’t work.

You’d think this feature would make parent.child.count or parent.child.length use this cache, instead of issuing a new COUNT query. You’d be wrong.

See the API for more information about :counter_cache

Another way to think of this is to say “the foreign key in table abc belongs_to table def”.

Gotchas

Watch your plural versus singular! If you’re going between has_many and belongs_to, it’s easy to get mixed up with plurals and singulars. belongs_to definitely uses singular parameters, and will not print very convenient errors if not put correctly.

If you are using belongs_to across namespaces, you will definitely need to use the :class_name and :foreign_key modifiers to belongs_to.

Say you are saying your Model belongs_to MySpace::OtherModel


class Model
belongs_to :other_model # will NOT work
# The following WILL work
belongs_to :other_model,
:class_name => "MySpace::OtherModel",
:foreign_key => "other_model_id"
end

分享到:
评论

相关推荐

    activemodel-associations, 用于普通 ruby 对象的has_many和belongs_to宏.zip

    activemodel-associations, 用于普通 ruby 对象的has_many和belongs_to宏 ActiveModel::Associations 用于普通 ruby 对象的has_many 和 belongs_to 宏。安装将此行添加到你的应用程序的Gemfile中:gem 'activemodel-

    ThinkPHP关联模型操作实例分析

    通常我们所说的关联关系包括下面三种: ◇ 一对一关联 : ONE_TO_ONE , 包括 HAS_ONE 和 BELONGS_TO ◇ 一对多关联 : ONE_TO_MANY , 包括 HAS_MANY 和 BELONGS_TO ◇ 多对多关联 : MANY_TO_MANY 关联定义 数据表...

    关于Rails中的表关联的程序

    Rails提供了四种基本的关联类型:` belongs_to`、` has_one`、` has_many` 和 `has_and_belongs_to_many`。这些关联允许我们建立对象之间的关系,从而在编程时简化数据的存取。 1. `belongs_to` 关联: 这种关联...

    Ruby on Rails中的ActiveRecord编程指南

     偏好 has_many :through 胜于 has_and_belongs_to_many。 使用 has_many :through 允许在 join 模型有附加的属性及验证   # 使用 has_and_belongs_to_many class User < ActiveRecord::Base has_and_...

    fixture_dependencies:SequelActiveRecord夹具加载器,用于处理依赖关系图

    支持many_to_one / belongs_to,one_to_many / has_many,many_to_many / has_and_belongs_to_many和has_one / one_to_one关联 以不违反外键约束的方式加载灯具的依赖关系图 有一个非常简单的API...

    thinkPHP3.x常量整理(预定义常量/路径常量/系统常量)

    本文整理总结了thinkPHP3.x常用的常量。分享给大家供大家参考,具体如下: 一、预定义常量 ...MANY_TO_MANY=4 MANY_TO_MANY 关联定义 THINK_VERSION 框架版本号 这些预定义常量不会随着环境的变化而变化。 二、

    one_plus_n_detector:Elixir库可帮助您使用Ecto检测应用程序中的1 + n个查询

    当您具有诸如has_many , has_one或belongs_to has_many的父子关系时,您可以通过一个查询加载父记录,然后为每条记录触发另一个SQL语句以加载相关的孩子。 假设您有以下Ecto模式 defmodule User do use Ecto . ...

    ruby声明式语法的实现例子

    has_many和belongs_to其实是Topic类的class method,标准写法是: 代码如下: class Topic < ActiveRecord::Base  Topic.has_many(:posts)  Topic.belongs_to(:user) end 那么has_many可以给我们带来什么呢?...

    微Reddit

    ApplicationRecord belongs_to :user has_many :commentsendclass User < ApplicationRecordhas_many :postshas_many :commentsendclass Comment < ApplicationRecord belongs_to :user belongs_to :postend...

    MangoDB:MongoDB KO 3.3(用于3.2、3.1和3.0检查分支)

    to,has_many和has_and_belongs_to_many 验证对象数据,包括嵌入式对象类表扩展的行为-例如Model_Ferrari扩展Model_Car扩展芒果。 其他ORM用户非常熟悉的非常简单的语法芒果数据库一个访问MongoDB的简单包装器。 将...

    basic-nested-forms-online-web-ft-110419

    使用有关主要对象的数据以及belongs_to和has_many关联构造嵌套的params哈希。 对关联数据使用常规键名(association_attributes)。 正确输入名称表单,以创建一个嵌套的params散列,该散列具有belongs_to和has_...

    ruby on rails在线考试系统

    5. Active Record关联:Rails中的ActiveRecord模型可以定义多种关联,如 belongs_to、has_many、has_one 和 has_and_belongs_to_many。在线考试系统中,试题可能 belongs_to 一个类别,考生 has_many 考试记录。 6....

    cfcalendar-backend

    has_many导师through任命 导师 has_many预约 has_many through预约的学生 约定 belongs_to学生 belongs_to导师 模型::变量 用户(学生和导师) 姓名 电子邮件 时区 约定 标题 学生卡 mentor_id 描述 状态[待审核,...

    Laravel开发-belongs-to-through

    在传统的Eloquent ORM中,我们有基本的`belongsTo`、`hasOne`、`hasMany`、`belongsToMany`等关系。`belongs-to-through`是这些基础关系的扩展,它通过一个中间模型来连接两个原本没有直接关系的模型。假设我们有三...

    displaying-has-many-through-rails-onl01-seng-pt-032320

    通过关联通过belongs_to,has_many和has_many查询关联。 遍历视图中的关联并显示主要实例的关联数据。 通过has_many识别联接模型。 概述 我们已经看到了如何使用简单的关联在Rails中向我们的用户显示数据,但是更...

    SinatraStreetFighter:欢迎使用我简化的Street Fighter模拟版本。 您可以选择一个角色,添加战斗动作以及史诗般的战斗开始的最后阶段!

    特征 楷模用户,角色,移动,舞台用户has_many:字符字符belongs_to :用户字符has_many :动作字符has_many :stages, through: ::moves 阶段has_and_belongs_to_many :移动阶段has_many :characters, ...

    atlas:Elixir的对象关系映射器

    通过以下方式添加模型关系,即belongs_to , has_many , has_many through: 其他SQL适配器 模式迁移 用法示例: defmodule User do use Atlas . Model @table :users @primary_key :id field :id , :integer...

    ruby多模型绑定

    在 Rails 中,有几种基本的关联类型:`belongs_to`, `has_many`, `has_one`, `has_and_belongs_to_many`等。这些关联类型可以用来表示不同模型之间的关系,例如一对多关系、多对一关系、多对多关系等。 #### 2.1 ...

    level-assoc:leveldb 的关系外键关联(hasMany,belongsTo)

    外键(hasMany、belongsTo、...) 例子 获取相关文档 var sub = require ( 'level-sublevel' ) ; var level = require ( 'level' ) ; var db = sub ( level ( 'hackerspaces.db' , { valueEncoding : 'json' } ) ) ...

    displaying-has-many-through-rails-v-000

    通过关联通过belongs_to,has_many和has_many查询关联。 遍历视图中的关联并显示主要实例的关联数据。 确定一个具有很多通过的联接模型。 概述 我们已经看到了如何使用简单的关联在Rails中向我们的用户显示数据,...

Global site tag (gtag.js) - Google Analytics