Не работайте поведения в Datamapper - PullRequest
0 голосов
/ 18 июля 2011

Я работаю с Синатрой. Это мои модели.

class Post
  include DataMapper::Resource
  property :id, Serial
  property :title, String
  property :body, Text
  property :posted, Boolean, :default  => true

  has n, :comments
  has n, :tags
end

class Comment
  include DataMapper::Resource
  property :id, Serial
  property :user, String
  property :body, Text
  property :posted, Boolean, :default  => false

  belongs_to :post
end

class Tag
  include DataMapper::Resource
  property :id, Serial
  property :tag, String
  property :weight, Integer, :default => 1

  belongs_to :post
end

Создать сообщение

tags = params[:tags].split(' ')
post = Post.new(:title=>params[:title],:body=>params[:body])
tags.each { |tg|
  post.tags << Tag.create(:tag=>tg)
}
redirect '/admin' if post.save

Но без тегов. Что мне нужно исправить?

1 Ответ

0 голосов
/ 19 июля 2011

Если вы используете отношение «один ко многим», вы должны создать теги с :post, установленным на post:

tags.each { |tg|
  Tag.create(:tag => tg, :post => post)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...