StatementInvalid error when adding a tagging system to a blog

31 views
Skip to first unread message

Nadia Vu

unread,
Feb 13, 2014, 1:57:48 AM2/13/14
to rubyonra...@googlegroups.com

Hi there, I'm very new to RoR please be kind.

I wasn't sure where to go but I was hoping I could find help here after days of hair pulling. Stackoverflow was not successful. 

I have followed the Official Beginners Guide to ROR and made a blog.

I wanted to take it further and add a tagging system to the blog. Im getting errors and I don't have enough knowledge of ruby to determine what is causing them. Any thoughts/help would be really appreciated.

So this is the error 

ActiveRecord::StatementInvalid in Posts#show

Showing /home/nadia/blog/app/views/posts/show.html.erb where line #8 raised:

SQLite3::SQLException: no such column: taggings.post_id: SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."post_id" = ?

Extracted source (around line #8):


 
 6<p>
 7Tags:
 8<% @post.tags.each do |tag| %>
 9<%= link_to tag.name, tag_path(tag) %>
10<% end %>
11</p>
 

 

 

And this a small chunk of the 

post_controller: 

def new
 27       @post = Post.new
 28    end
 29    def create
 30       @post = Post.new(post_params)
 31 
 32         if @post.save
 33             redirect_to action: :show, id: @post.id
 34          else
 35             render 'new'
 36          end
 37    end
 38 
 39    def destroy
 40        @post = Post.find(params[:id])
 41        @post.destroy
 42 
 43     redirect_to action: :index
 44     end

This is my tag_controller


  1 class TagsController < ApplicationController
  2 
  3     def show
  4       @tag = Tag.find(params[:id])
  5 end
~                                                                                      

my tag.rb

  1 class Tag < ActiveRecord::Base
  2   has_many :taggings
  3   has_many :posts, through: :taggings
  4 
  5 end 

post.rb

  1 class Post < ActiveRecord::Base
  2 
  3     has_many :comments, dependent: :destroy
  4     has_many :taggings
  5     has_many :tags, through: :taggings
  6     validates :title,
  7             presence: true,
  8                length: { minimum: 5 }
  9 
 10   def tag_list
 11    self.tags.collect do |tag|
 12       tag.name
 13   end.join(", ")
 14   end
 15   def tag_list=(tags_string)
 16     tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq
 17     new_or_found_tags = tag_names.collect { |name| Tag.find_or_create_by(name: name) }
 18     self.tags = new_or_found_tags
 19   end
 20 end
~       

and my posts_helper

  1 module PostsHelper
  2   def post_params
  3     params.require(:post).permit(:title, :body, :tag_list)
  4   end
  5 end

Please let me know if I can add any more code that you would need to see

Please please help. I really need to get out of this little rut. And any comments would be appreciated.

 

Colin Law

unread,
Feb 13, 2014, 5:12:38 PM2/13/14
to rubyonra...@googlegroups.com
On 13 February 2014 06:57, Nadia Vu <winte...@gmail.com> wrote:
> Hi there, I'm very new to RoR please be kind.
>
> I wasn't sure where to go but I was hoping I could find help here after days
> of hair pulling. Stackoverflow was not successful.
>
> I have followed the Official Beginners Guide to ROR and made a blog.
>
> I wanted to take it further and add a tagging system to the blog. Im getting
> errors and I don't have enough knowledge of ruby to determine what is
> causing them. Any thoughts/help would be really appreciated.
>
> So this is the error
>
> ActiveRecord::StatementInvalid in Posts#show
>
> Showing /home/nadia/blog/app/views/posts/show.html.erb where line #8 raised:
>
> SQLite3::SQLException: no such column: taggings.post_id: SELECT "tags".*

It says there is no column post_id in the table taggings. Is it that
you do not think
it should be looking for that column or is it that you know there
should be but think that
it does exist?

You have neglected to show us taggings.rb but I presume it contains
belongs_to post.

Colin

Nadia Vu

unread,
Feb 17, 2014, 2:52:40 AM2/17/14
to rubyonra...@googlegroups.com
Oh yes sorry
This is my Taggings.rb

  1 class Tagging < ActiveRecord::Base
  2   belongs_to :tag
  3   belongs_to :posts
  4 end

 

Sridhar Vedula

unread,
Feb 17, 2014, 4:47:35 AM2/17/14
to rubyonra...@googlegroups.com
It is belongs_to :post   ----------- not posts

Nadia Vu

unread,
Feb 17, 2014, 7:13:24 AM2/17/14
to rubyonra...@googlegroups.com
Hi, thank you for that. 
I have made that change. From 

belongs_to :tag
belongs_to :posts

to 

belongs_to :tag
belongs_to :post



However. I am still getting the same error.
Looking at many similar open stackoverflow answers and questions I honestly thought the pluralization was going to fit it. But unfortunately it still hasn't.




--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/GQwj6Mrx2CE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ab6b8df7-5eed-423a-aab1-42468426e532%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Colin Law

unread,
Feb 17, 2014, 9:25:03 AM2/17/14
to rubyonra...@googlegroups.com
On 17 February 2014 12:13, Nadia Vu <winte...@gmail.com> wrote:
> Hi, thank you for that.
> I have made that change. From
>
> belongs_to :tag
> belongs_to :posts
>
> to
>
> belongs_to :tag
> belongs_to :post
>
>
>
> However. I am still getting the same error.
> Looking at many similar open stackoverflow answers and questions I honestly
> thought the pluralization was going to fit it. But unfortunately it still
> hasn't.

Are you still getting the error saying that there is no column post_id
in the table taggings? I askied this in my first reply and you did
not answer. If it is still saying that have you checked that there is
such a column?

Colin
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/CAOWWFs0ZRabxQYrNJYMS8Gmh8D%3Dwt4HXV5MhSZkkoruCESdmKQ%40mail.gmail.com.

Nadia Vu

unread,
Feb 17, 2014, 7:23:16 PM2/17/14
to rubyonra...@googlegroups.com
Yep. All fixed now. 
I checked for the columns like you asked. And I found out that the typo/pluralization in the Taggings.rb file named the column wrong
ie:

belongs_to :posts 

made the column name posts_id as opposed to post_id


after fixing the typo, I rolled back the migration, and ran the migration again to change the name of the table.
AND NOW IT WORKS!
Thank you so much everyone! I really appreciate all the help!


Colin Law

unread,
Feb 18, 2014, 5:13:08 AM2/18/14
to rubyonra...@googlegroups.com
On 18 February 2014 00:23, Nadia Vu <winte...@gmail.com> wrote:
> Yep. All fixed now.
> I checked for the columns like you asked. And I found out that the
> typo/pluralization in the Taggings.rb file named the column wrong
> ie:
>
> belongs_to :posts
>
> made the column name posts_id as opposed to post_id
>
>
> after fixing the typo, I rolled back the migration, and ran the migration
> again to change the name of the table.
> AND NOW IT WORKS!
> Thank you so much everyone! I really appreciate all the help!

Error messages can be very confusing and it is easy to miss the one
that is important in a batch of other messages. It is always worth
looking carefully through the errors in the hope of finding one that
is useful, in this case the one saying that the column was missing.

Colin
Reply all
Reply to author
Forward
0 new messages