0

私のコードは以下の通りです

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")
4

1 に答える 1

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)
于 2011-12-10T13:04:27.597 に答える