我用Ryan Bates的source code for railscasts#141创建了一个简单的购物车.在其中一次迁移中,他列出了

class CreateProducts < ActiveRecord::Migration
  def self.up
    create_table :products do |t|
      t.belongs_to :category
      t.string :name
      t.decimal :price
      t.text :description
      t.timestamps
    end
  end

  def self.down
    drop_table :products
  end
end

以下是产品型号:

class Product < ActiveRecord::Base
 belongs_to :category
end

t.belongs_to :category线是多少?这是t.integer category_id的别名吗?

推荐答案

t.belongs_to :category只是协会中通过的special helper method根铁轨.

如果你看source codebelongs_to实际上是references的别名

Ruby-on-rails相关问答推荐

Ruby线程使用互斥处理并发问题

带有rails 7.1的Lightbox2未初始化

Ruby on rails Admin-有没有一种方法可以在成对的管理控制器类中合并、重用应用程序控制器类?

有没有一种方法可以点击一个按钮来冒泡到父元素?

rails turbo frame 显然在更新,但浏览器窗口保持不变

Rails - Comment.count没有改变1

未初始化的常量 ActionDispatch::Session::EncryptedCookieStore (NameError)

如何从 Rails 路由中删除控制器名称?

fields_for 表单构建器对象为零

禁用 RVM 或使用没有 RVM 安装的 Ruby?

如何将字符串转换为类方法?

Rails Rake 任务 - 访问模型类

我可以在 twitter-bootstrap popover 数据内容中使用 html 标签吗?

在 Devise on Rails beta 3 中创建管理员用户

'respond_to' 格式语句中的 `:location => ...` 和 `head :ok` 是什么意思?

获取完整的 rails 控制器名称,包括命名空间

活动管理员:仅自定义新表单

向 Rails 模型添加外键

Rails:为什么 with_exclusive_scope 受保护?关于如何使用它的任何好的做法?

为什么 Rails 需要 JavaScript 运行时?