新手 Rails HABTM 关联不工作
我是 Rails 新手,尝试在 orders 和 items 之间创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,为什么不直接使用belongs_to和has_many呢?例如:
至于为什么没有得到预期的结果你可以尝试:
或者更好
First of all, why don't you just use belongs_to and has_many? For example:
As for why you don't get expected results you can try:
or better