Ruby on rails 在ActiveRecord中建模wordpress taxonmy

Ruby on rails 在ActiveRecord中建模wordpress taxonmy,ruby-on-rails,wordpress,Ruby On Rails,Wordpress,我需要访问rails中的wordpress数据库。是的,很有趣。我已经能够获得博客内容,但我需要访问每个博客文章的“类别”。这些类别有两个连接。获取此内容的原始sql如下所示: select term.name from wp_term_relationships rel inner join wp_term_taxonomy as tt on rel.term_taxonomy_id = tt.term_taxonomy_id inner join wp_terms as term on te

我需要访问rails中的wordpress数据库。是的,很有趣。我已经能够获得博客内容,但我需要访问每个博客文章的“类别”。这些类别有两个连接。获取此内容的原始sql如下所示:

select term.name from wp_term_relationships rel
inner join wp_term_taxonomy as tt
on rel.term_taxonomy_id = tt.term_taxonomy_id
inner join wp_terms as term
on term.term_id = tt.term_id
where object_id = 123
and taxonomy = 'category';
我已经有了WpTerm和WpPost课程。我现在需要弄清楚如何使用AR将这些连接在一起


谢谢

这似乎对我有用(我正在从Wordpress加载辅助数据库,因此
建立连接()
调用并覆盖
表名

class Term < ActiveRecord::Base
   establish_connection "wordpress-#{Rails.env}"
   self.table_name = "wp_terms"

   has_one :term_taxonomy
end


class TermTaxonomy < ActiveRecord::Base
   establish_connection "wordpress-#{Rails.env}"
   self.table_name = "wp_term_taxonomy"

   belongs_to :term
   has_many :term_relationship
end

class TermRelationship < ActiveRecord::Base
   establish_connection "wordpress-#{Rails.env}"
   self.table_name = "wp_term_relationships"

   belongs_to :post, :foreign_key => "object_id"
   belongs_to :term_taxonomy
   has_one :term, :through => :term_taxonomy
end

class Post < ActiveRecord::Base
   establish_connection "wordpress-#{Rails.env}"
   self.table_name = "wp_posts"

   has_many :term, :through => :term_relationship
   has_many :term_relationship, :foreign_key => "object_id"
   has_one  :postmeta

   # we only care about published posts for notifications
   default_scope where("post_type = 'post' and post_status = 'publish'")
end

class Postmeta < ActiveRecord::Base
   establish_connection "wordpress-#{Rails.env}"
   self.table_name = "wp_postmeta"

   belongs_to :post
end

我在Activerecord中映射Wordpress数据模型时遇到了同样的挑战

Ari Gesher的回答非常有帮助,但我认为香草ruby类WPCategory是没有必要的。另外,看看Wordpress:Term有很多术语分类法,而不是一个。因此我的Wordpress模型是这样的(注意,我使用的是Rails 2.3):

类术语{:taxonomy=>'categority'}
结束
类TermRelationship'object\u id'
属于:术语分类法
有一个:术语,:通过=>:term\u分类法
结束
类Post'object\u id'
有很多:术语分类法,:至=>:术语关系
默认_作用域:条件=>{:post_类型=>'post',:post_状态=>'publish'}
def类别
术语分类。分类。地图(&:术语)
结束
结束
我用TermTaxonomy中的命名范围和Post中的categories方法替换了Ari Gesher的WPCategory。

另一种方法:

require 'mysql2'
require 'active_record'
require 'composite_primary_keys'
require 'pry'

spec = {
  adapter:  :mysql2,
  username: :root,
  database: '7fff_com'
}

ActiveRecord::Base.establish_connection(spec)

class Post < ActiveRecord::Base
  self.table_name = 'wp_posts'
  self.primary_key = 'ID'
  has_many :post_term_taxonomies, :foreign_key => :object_id
  has_many :term_taxonomies, through: :post_term_taxonomies

  def categories
    term_taxonomies.where(taxonomy: 'category').map { |t| t.term.name }
  end
  def tags
    term_taxonomies.where(taxonomy: 'post_tag').map { |t| t.term.name }
  end
end

class Term < ActiveRecord::Base
  self.table_name = 'wp_terms'
  self.primary_key = 'term_id'
  has_many :post_term_taxonomies, :foreign_key => :term_taxonomy_id
  has_many :posts, through: :post_term_taxonomies
  has_one :term_taxonomy, primary_key: :term_id
end

class PostTermTaxonomy < ActiveRecord::Base
  self.table_name = 'wp_term_relationships'
  self.primary_keys = :object_id, :term_taxonomy_id
  belongs_to :post, foreign_key: :object_id
  belongs_to :term_taxonomy, foreign_key: :term_taxonomy_id
end

class TermTaxonomy < ActiveRecord::Base
  self.table_name = 'wp_term_taxonomy'
  self.primary_key = 'term_taxonomy_id'
  belongs_to :term
end

class Term < ActiveRecord::Base
  establish_connection Rails.configuration.database_configuration["wordpress_#{Rails.env}"]
  set_table_name 'wp_terms'
  set_primary_key 'term_id'

  has_many :term_taxonomies
end

class TermTaxonomy < ActiveRecord::Base
  establish_connection Rails.configuration.database_configuration["wordpress_#{Rails.env}"]
  set_table_name 'wp_term_taxonomy'
  set_primary_key 'term_taxonomy_id'

  belongs_to :term
  has_many :term_relationships

  named_scope :categories, :conditions => {:taxonomy => 'category'}
end

class TermRelationship < ActiveRecord::Base
  establish_connection Rails.configuration.database_configuration["wordpress_#{Rails.env}"]
  set_table_name 'wp_term_relationships'
  set_primary_key 'object_id'

  belongs_to :post, :foreign_key => 'object_id'
  belongs_to :term_taxonomy
  has_one :term, :through => :term_taxonomy
end

class Post < ActiveRecord::Base
  establish_connection Rails.configuration.database_configuration["wordpress_#{Rails.env}"]
  set_table_name 'wp_posts'
  set_primary_key 'ID'

  has_many :term_relationships, :foreign_key => 'object_id'
  has_many :term_taxonomies, :through => :term_relationships

  default_scope :conditions => {:post_type => 'post', :post_status => 'publish'}

  def categories
    term_taxonomies.categories.map(&:term)
  end
end
require 'mysql2'
require 'active_record'
require 'composite_primary_keys'
require 'pry'

spec = {
  adapter:  :mysql2,
  username: :root,
  database: '7fff_com'
}

ActiveRecord::Base.establish_connection(spec)

class Post < ActiveRecord::Base
  self.table_name = 'wp_posts'
  self.primary_key = 'ID'
  has_many :post_term_taxonomies, :foreign_key => :object_id
  has_many :term_taxonomies, through: :post_term_taxonomies

  def categories
    term_taxonomies.where(taxonomy: 'category').map { |t| t.term.name }
  end
  def tags
    term_taxonomies.where(taxonomy: 'post_tag').map { |t| t.term.name }
  end
end

class Term < ActiveRecord::Base
  self.table_name = 'wp_terms'
  self.primary_key = 'term_id'
  has_many :post_term_taxonomies, :foreign_key => :term_taxonomy_id
  has_many :posts, through: :post_term_taxonomies
  has_one :term_taxonomy, primary_key: :term_id
end

class PostTermTaxonomy < ActiveRecord::Base
  self.table_name = 'wp_term_relationships'
  self.primary_keys = :object_id, :term_taxonomy_id
  belongs_to :post, foreign_key: :object_id
  belongs_to :term_taxonomy, foreign_key: :term_taxonomy_id
end

class TermTaxonomy < ActiveRecord::Base
  self.table_name = 'wp_term_taxonomy'
  self.primary_key = 'term_taxonomy_id'
  belongs_to :term
end
[1] pry(main)> post = Post.find(764)
[2] pry(main)> post.categories
=> ["Technology", "Code"]
[3] pry(main)> post.tags
=> ["boxen"]