Hi, (message did not seem to be posted via Google webform)
I'm trying to use Rails to peek into some WordPress (weblog) database,
using ActiveRecord. Retrieving posts works elegantly, even without one
single line of code. Unfortunately, I can't retrieve associated data
correctly, for example getting the categories of a post.
The relevant WordPress tables and fields are:
Obvisously, wp_post2cat is the join table, linking many posts to many
In my Rails app, I've set up the following models:
class Post < ActiveRecord::Base
has_and_belongs_to_many :categories, :join_table => "wp_post2cat"
class Category < ActiveRecord::Base
has_and_belongs_to_many :posts, :join_table => "wp_post2cat"
With this, a correct database.yml and the correct table prefix "wp_",
I can correctly retrieve posts or categories:
p = Post.find(4812)
c = Category.find(10)
(For the sake of argument, trust me that post 4812 is associated to
p.methods() shows that this object also has a method categories(), and
c.methods() shows a posts() method.
The problem is that is isn't working:
If I look to the resulting SQL-query, I see:
SELECT * FROM wp_categories
INNER JOIN wp_post2cat ON wp_categories.cat_id = wp_post2cat.category_id
WHERE (wp_post2cat.post_id = NULL )
I assume that the problem is in the WHERE-clause: post_id shouldn't be
tested against NULL, but against 4812, the id of Post p. If I replace
NULL with 4812, I do get the correct Category record from the query.
Could someone locate the error in my setup, or provide pointers for
Many thanks, kind regards,
(And sorry if this message is a dupe accidentally. Posting new topics
via the Google Groups web interface doesn't seem to be working.)