我的代码在
下面class City
include DataMapper::Resource
has n, :forums
property :id, Serial
property :name, String
property :parent_state, String
property :url, String, :length => 255
end
class Category
include DataMapper::Resource
has n, :forums
property :id, Serial
property :name, String
property :url, String, :length => 255
end
class Forum
include DataMapper::Resource
belongs_to :city
belongs_to :category
has n, :posts
property :id, Serial
property :rss, String, :length => 255
end
class Post
include DataMapper::Resource
belongs_to :forum
property :id, Serial
property :title, String, :length => 255
property :date, Date
property :time, Time
property :body, Text
property :url, String, :length => 255
property :email, String, :length => 255
end
我可以轻松创建一个新的城市......(这是一个我认为你真的不想看的循环内部):
City.create(:parent_state => state, :name => citylink.content, :url => citylink.get_attribute('href'))
但是对于我的生活,我无法弄清楚我是如何创建一个新的论坛(所有论坛都是RSS属性)。我尝试用100种不同的方式编写它并且它出错或者它只是不写入数据库,我假设因为没有给出任何关联所以它拒绝写它。
我已经阅读了很多DM教程和写作,但我仍然不知道自己会做什么。
任何帮助都非常感谢!
这是我最近的愚蠢样本测试..可能会离开......
city = City.get(:name => cityname)
Forum.create(:city => city, :rss => "this works now")
答案 0 :(得分:1)
应该是:
forum = city.forums.create(:rss => "whatever")
如果这不起作用,请尝试检查错误,看是否有任何明显被忽视的迹象:
forum.errors.full_messages
(假设你有dm-validations)
编辑|顺便说一句,这是无效的:
city = City.get(:name => cityname)
你可能想要:
city = City.first(:name => cityname)
或
cities = City.all(:name => cityname)
使用.get
时,您只能传递主键,如下所示:
city = City.get(1)