【问题标题】:Sinatra: Trouble with associating class instances having DataMapper One-To-Many-Through relationSinatra:关联具有 DataMapper 一对多关系的类实例时遇到问题
【发布时间】:2015-08-02 19:07:00
【问题描述】:

我正在尝试为 cms 构建管理后端。我有几个模型类,其中两个是PostTag。他们连接throughTagging类。

后模型

class Post
  include DataMapper::Resource

  property :id,           Serial
  property :title,        String, :required => true
  property :body,           Text,   :required => true
  property :is_blog_post, Boolean, :default => true
  property :viewed,             Integer, :default => 0
  property :featured,     Boolean, :default => false
  property :created_at,     DateTime
  property :updated_at,     DateTime

  belongs_to :author
  belongs_to :category
  has n, :comments
  has n, :taggings
    has n, :tags, :through => :taggings

 #some other methods


end

标签模型

class Tag
    include DataMapper::Resource

    property :id,               Serial
    property :name,             String, :unique =>true

    has n, :taggings
    has n, :posts, :through => :taggings

   #some methods
end

标记

class Tagging
    include DataMapper::Resource

    belongs_to :tag, :key => true
    belongs_to :post, :key => true
end

routes.rb 中用于创建帖子的相关代码 (the whole program structure here)

get '/articles/new' do
    @cats = Category.all
    @article = Post.new
    erb :post_form
end


post '/articles' do
    @article = Post.create(
        :title => params[:title],
        :body => params[:body],
        :featured => params[:featured],
        :category_id =>params[:category_id],
        :author_id => session[:user].id,
        :is_blog_post => false
        )

    taginputs = params[:tags]
    taginputs.split(", ").each do |newtag|
        nt = Tag.first_or_create(:name=> newtag)
        @article.tags << nt
    end

    if @article.saved?
        redirect to ('/articles')
    else
        redirect to ("/articles/new")   
    end

end

这是 post_form.erb 部分

<div style="margin-left:50px;">
<legend>New article</legend>

<div class="alert alert-info" role="alert">
    <p> <!-- flash notice will be here --> </p>
</div>
<form action='/articles' method="post">
<div class="form-group">
    <p><label>Title</label></p>
    <input type="text" name="title" value="<%= @article.title unless @article.title.nil? %>">   
</div>
<div class="form-group">
    <p><label>Body</label></p>
    <textarea name="body"><%= @article.body unless @article.body.nil? %></textarea> 
</div>
<% if session[:user].is_admin? %>
<div class="form-group">
    <input type="radio" name="featured" value="1"><p>Featured</p>
    <input type="radio" name="featured" value="0"><p>Ordinary</p>
</div>
<% end %>

<div class="form-group">
    <input type="text" name="tags" value="<%= @article.post_tags unless @article.post_tags.nil?%>">
</div>

<div class="form-group">
    <select name="category_id">
        <% @cats.each do |cat|%>
            <option value="<%= cat.id %>"><%= cat.name %></option>
        <% end %>
    </select>
</div>
<div class="form-group">
    <input type="submit" value="Submit">
</div>

</form>
</div>

当我提交表单时,它会按照我的预期创建Post 实例。它还可以很好地创建Tag 实例。但它不会将tags 与该文章相关联。

当我在irb 中测试相同的逻辑(当然是根据我自己)时,

2.2.0 :002 > p8 = Post.get(8)
 => #<Post @id=8 @title="What is lorem ipsum?" @body=<not loaded> @is_blog_post=true @viewed=0 @featured=false @created_at=#<DateTime: 2015-08-02T23:11:31+05:00 ((2457237j,65491s,0n),+18000s,2299161j)> @updated_at=#<DateTime: 2015-08-02T23:11:31+05:00 ((2457237j,65491s,0n),+18000s,2299161j)> @author_id=1 @category_id=1> 
2.2.0 :003 > sometags = "Lorem Ipsum, Lorem, Ipsum, Dolores, O'Riordan"
 => "Lorem Ipsum, Lorem, Ipsum, Dolores, O'Riordan" 
2.2.0 :004 > sometags.split(", ").each do |newtags|
2.2.0 :005 >     nt = Tag.first_or_create(:name => newtags)
2.2.0 :006?>   p8.tags << nt
2.2.0 :007?>   end
 => ["Lorem Ipsum", "Lorem", "Ipsum", "Dolores", "O'Riordan"] 
2.2.0 :008 > p8.tags
 => [#<Tag @id=29 @name="Lorem Ipsum">, #<Tag @id=30 @name="Lorem">, #<Tag @id=31 @name="Ipsum">, #<Tag @id=32 @name="Dolores">, #<Tag @id=34 @name="O'Riordan">] 

它也有效。

我错过了什么还是什么?

【问题讨论】:

    标签: ruby sinatra one-to-many ruby-datamapper


    【解决方案1】:

    幸运的是我刚刚看到,我忘记为新创建的Post 实例调用save! 方法。在@article.tags &lt;&lt; nt之后添加@article.save!

    ...
    @article.tags << nt
    @article.save!
    end
    ...
    

    现在好了:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      相关资源
      最近更新 更多