Ruby on rails 轨道固定装置:有多个和:属于
如何在.yml中为has_许多且属于变量创建示例数据 这是一个将这些文件添加到终端中的简单rails new lab命令中的示例。我真的不知道如何用英语解释这个。但我希望我的代码能显示足够的细节,让大家明白这一点 man.rbRuby on rails 轨道固定装置:有多个和:属于,ruby-on-rails,ruby,fixtures,Ruby On Rails,Ruby,Fixtures,如何在.yml中为has_许多且属于变量创建示例数据 这是一个将这些文件添加到终端中的简单rails new lab命令中的示例。我真的不知道如何用英语解释这个。但我希望我的代码能显示足够的细节,让大家明白这一点 man.rb class Man < ActiveRecord::Base attr_accessible :name has_many :items end items.yml one: name: ItemOne two: name: ItemTwo i
class Man < ActiveRecord::Base
attr_accessible :name
has_many :items
end
items.yml
one:
name: ItemOne
two:
name: ItemTwo
item_one:
name: ItemOne
man: man_one
item_two:
name: ItemTwo
man: man_one
item_three:
name: ItemThree
man: man_two
man_test.rb
require 'test_helper'
class ManTest < ActiveSupport::TestCase
def test_man
Man.all.each do |man|
puts man.name.to_s + ": " + man.items.to_s
end
assert true
end
end
需要“测试助手”
类ManTest
我想您是在询问有关固定装置的问题。你是这样做的:
#men.yml
first_man:
name: 'One'
#items.yml
first_item:
name: 'First item'
man: first_man
看看固定装置,你可以做如下事情:
男青年
one:
name: ManOne
#items: one, two
two:
name: ManTwo
#items: one, two
man_one:
name: ManOne
man_two:
name: ManTwo
items.yml
one:
name: ItemOne
two:
name: ItemTwo
item_one:
name: ItemOne
man: man_one
item_two:
name: ItemTwo
man: man_one
item_three:
name: ItemThree
man: man_two
更新
表列中似乎没有人员id
。您应该创建一个迁移来执行此操作:
rails g migration AddManIdToItem man_id:integer
然后运行迁移:bundle exec rake db:migrate
我在您所做的更改中添加的。然后,我将man(:man_one)放入man_test.rb中。我得到了一个errorActiveRecord::StatementInvalid:SQLite3::SQLException:表项没有名为man_id的列:插入到“items”中(“name”、“created_at”、“updated_at”、“id”、“man_id”)值('ItemOne”、“2014-03-27 23:09:59”、“2014-03-27 23:09:59”、“494534085、82093985)项目中没有“人员id”列。我已经更新了答案。