Search code examples
rubysinatraone-to-manyruby-datamapper

Sinatra: Trouble with associating class instances having DataMapper One-To-Many-Through relation


I'm trying to build an admin backend for a cms. I have several model classes, and two of them are Post and Tag. They are connected through Tagging class.

Post model

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

Tag model

class Tag
    include DataMapper::Resource

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

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

   #some methods
end

Tagging

class Tagging
    include DataMapper::Resource

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

relevant code from routes.rb to create a post (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

and this is post_form.erb partial

<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>

When I submit the form, it creates the Post instance as I expected. It also does create Tag instances very well. But it doesn't associate the tags with that article.

when I test the same logic (according to me, of course) in 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">] 

it also works.

Am I missing something or what?


Solution

  • Luckily I just saw that, I've forgotten to call save! method for newly created Post instance. Added @article.save! after @article.tags << nt

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

    and it's ok now :)