Problem with relations

Hi guys, I’m following the Beginning Rails 3 Book, I’ve problems with
the
many to many relationship between Article-Category, when I try to create
or
show a article.

Please help me, I’m new with Rails.

Regards,

ActiveRecord::StatementInvalid in Articles#new

Showing I:/Labs/RailsProjects/blog/app/views/articles/_form.html.erb
where
line #27 raised:

SQLite3::SQLException: no such column:
articles_categories.category_id: SELECT “categories”.id FROM
“categories” INNER JOIN “articles_categories” ON “categories”.id =
“articles_categories”.category_id WHERE
(“articles_categories”.article_id = NULL )

Extracted source (around line #27):

24: <%= f.label “Categories” %>

25: <% for category in Category.all %>

26: <%= check_box_tag ‘article[category_ids][]’, category.id,

27: @article.category_ids.include?(category.id), :id =>
dom_id(category) %>

28: <%= label_tag dom_id(category), category.name, :class =>
“check_box_label” %>

29: <% end %>

30:

Trace of template inclusion: app/views/articles/new.html.erb

Rails.root: I:/Labs/RailsProjects/blog
Application Trace http://localhost:3000/articles/new#
| Framework Trace http://localhost:3000/articles/new# | Full
Tracehttp://localhost:3000/articles/new#

app/views/articles/_form.html.erb:27:in `block (2 levels) in
_app_views_articles__form_html_erb___743271537_14143752__942053146’

app/views/articles/_form.html.erb:25:in `each’

app/views/articles/_form.html.erb:25:in `block in
_app_views_articles__form_html_erb___743271537_14143752__942053146’

app/views/articles/_form.html.erb:1:in
`_app_views_articles__form_html_erb___743271537_14143752__942053146’

app/views/articles/new.html.erb:6:in
`_app_views_articles_new_html_erb__987918893_24332892_280057368’

app/controllers/articles_controller.rb:20:in `new’

Request

Parameters
:

None

Show session dump http://localhost:3000/articles/new#

Show env dump http://localhost:3000/articles/new#
Response

Headers
:

None


TSU. Amador Cuenca

On 14 December 2010 15:35, Amador Antonio C. [email protected]
wrote:

SQLite3::SQLException: no such column: articles_categories.category_id:

And has your articles_categories table got a column category_id?
If you think it has then how do you know?
If you still think it has show us db/schema.rb

Colin

Hi Amador,

Looking at the application code you attached, your migration has the
wrong code in db/migrate/
20101208225837_create_articles_categories.rb.

Basically, in the book ([Beginning Rails 3] Listing 5-12. The db/migrate/20100307002156_create_articles_categories.rb file · GitHub), we use:

t.references :category

and not

t.references :categories

What you can do to fix this is to rollback your database using: ‘rake
db:rollback’, until you’re back before this particular migration.
Update the file, and then run ‘rake db:migrate’ and you should be good
to go.

I hope this fixes your problem.

Cheers,

Rida Al Barazi
Twitter: @rida
Blog: http://rida.me
Book: http://beginningrails.com

Thanks, it works. Now I can continue with the book.

Regards,


TSU. Amador Cuenca