新手 Rails HABTM 关联不工作

发布于 2024-09-10 06:41:32 字数 1856 浏览 4 评论 0原文

我是 Rails 新手,尝试在 ordersitems 之间创建 has_and_belongs_to_many 关系。

class Order < ActiveRecord::Base
  has_and_belongs_to_many :items
end

class Item < ActiveRecord::Base
  has_and_belongs_to_many :orders
end

订单迁移(未显示。非常基本)

OrderItems 迁移:

class CreateItems < ActiveRecord::Migration
  def self.up
    create_table :items do |t|
      t.string :name
      t.decimal :price

      t.timestamps
    end

    create_table :items_orders, :id => false do |t|
      t.references :item, :order
    end
  end

  def self.down
    drop_table :items
    drop_table :items_orders
  end
end

在脚本/控制台中,我试图“证明”这种关系有效,但要么我对 Ruby 的理解不好(可能),要么我的模型不好是。

$ script/console
Loading development environment (Rails 2.3.5)
>> o = Order.new
=> #<Order id: nil, name: nil, created_at: nil, updated_at: nil>
>> o.name = 'first order'
=> "first order"
>> o.save
=> true
>> o.items
=> []
>> i1 = o.items.new
=> #<Item id: nil, name: nil, price: nil, created_at: nil, updated_at: nil>
>> i1.name = 'some widget'
=> "some widget"
>> i1.price = 12.50
=> 12.5
>> i1.save
=> true
>> o.items
=> []
>> o.items.first
=> nil

查看development.sqlite3 数据库:

$ sqlite3 development.sqlite3 
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
items              items_orders       orders             schema_migrations
sqlite> select * from items_orders;
sqlite> .schema items_orders
CREATE TABLE "items_orders" ("item_id" integer, "order_id" integer);
sqlite>

什么都没有!

我知道这是显而易见的……但对我来说不是……无论如何,在这个阶段……

我错过/搞砸了什么?

I'm new to Rails and trying to create a has_and_belongs_to_many relationship between orders and items.

class Order < ActiveRecord::Base
  has_and_belongs_to_many :items
end

class Item < ActiveRecord::Base
  has_and_belongs_to_many :orders
end

Migration for Orders (not shown. very basic)

Migration for OrderItems:

class CreateItems < ActiveRecord::Migration
  def self.up
    create_table :items do |t|
      t.string :name
      t.decimal :price

      t.timestamps
    end

    create_table :items_orders, :id => false do |t|
      t.references :item, :order
    end
  end

  def self.down
    drop_table :items
    drop_table :items_orders
  end
end

In script/console I'm trying to "prove" that relationship works but either my understanding of Ruby is bad (likely) or my model is.

$ script/console
Loading development environment (Rails 2.3.5)
>> o = Order.new
=> #<Order id: nil, name: nil, created_at: nil, updated_at: nil>
>> o.name = 'first order'
=> "first order"
>> o.save
=> true
>> o.items
=> []
>> i1 = o.items.new
=> #<Item id: nil, name: nil, price: nil, created_at: nil, updated_at: nil>
>> i1.name = 'some widget'
=> "some widget"
>> i1.price = 12.50
=> 12.5
>> i1.save
=> true
>> o.items
=> []
>> o.items.first
=> nil

looking in the development.sqlite3 database:

$ sqlite3 development.sqlite3 
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
items              items_orders       orders             schema_migrations
sqlite> select * from items_orders;
sqlite> .schema items_orders
CREATE TABLE "items_orders" ("item_id" integer, "order_id" integer);
sqlite>

Nothing!

I know it's obvious...but not to me...at this stage anyway...

what have I missed/screwed up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷途知返 2024-09-17 06:41:32

首先,为什么不直接使用belongs_to和has_many呢?例如:

class Order < ActiveRecord::Base
  has_many :items
end

class Item < ActiveRecord::Base
  belongs_to :order
end

至于为什么没有得到预期的结果你可以尝试:

order = Order.new
order.save
item = Item.new
item.order = order
item.save

或者更好

order = Order.create(myordercolumn => "whatever")
order.items.create(:name => "some widget")

First of all, why don't you just use belongs_to and has_many? For example:

class Order < ActiveRecord::Base
  has_many :items
end

class Item < ActiveRecord::Base
  belongs_to :order
end

As for why you don't get expected results you can try:

order = Order.new
order.save
item = Item.new
item.order = order
item.save

or better

order = Order.create(myordercolumn => "whatever")
order.items.create(:name => "some widget")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文