2011-09-13 7 views
1

私はレールでwordpressデータベースにアクセスする必要があります。うん、楽しい時。私はブログの内容はOKですが、各ブログ投稿の "カテゴリ"にアクセスする必要があります。カテゴリは2つの結合から離れています。このコンテンツを取得するための生のSQLは次のとおりです。ActiveRecordのワードプレスタクソミーのモデル化

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を使ってこれらをどのように結びつけるかを理解する必要があります。

おかげ

答えて

1

これは(私はそれゆえ、二次データベースとしてのWordpressからestablish_connection()コールとオーバーライドtable_nameをロードしています私のために働いているようだ。

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 

私はその後でカテゴリを包みます

class WPCategory 
    attr_accessor :id 
    attr_accessor :name 
    attr_accessor :description 
    attr_accessor :term 

    def self.categories() 
     categories = Term.all() 
     categories = categories.select{|term| term.term_taxonomy.taxonomy == "category"} 
     return categories.map{|term| WPCategory.new(term)} 
    end 

    def self.category(id=nil) 
     if id 
     term = Term.find(id) 
     if term.term_taxonomy.taxonomy == "category" 
      return WPCategory.new(term) 
     end 
     end 
     return nil 
    end 

    def initialize(term) 
     @id = term.term_id 
     @name = term.name 
     @description = term.term_taxonomy.description 
     @term = term 
    end 

    def to_s 
     return "Wordpress Category: '#{@name}' (id=#{@id})" 
    end 

end 
1

私はWordpressデータモデルをActiにマッピングするのと同じ課題を抱えていましたベレコード。

アリGesherの答えは非常に役に立ちましたが、私はバニラルビークラスWPCategoryは必要ではないと思います。また、WordpressのERDを見てください。Termには、1つではなく多くのterm taxonomiesがあります。私はTermTaxonomyでnamed_scopeとポストのカテゴリ法によりアリGesherのWPCategoryを置き換え

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 

:だから、私のWordpressのモデルは、この(私はRailsの2.3を使用ノート)のように見えます。

0

別の方法:

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

http://7fff.com/2015/01/moving-from-wordpress-to-middleman-part-i/

を参照してください。
関連する問題