我有一个带有三个主要模型的rails API应用程序:User,Article和Annotation(annotations =创建文章部分的注释)和两个支持连接模型:Article_View_History和Annotation_View_History
用户可以创建文章以及查看文章。 article_view_history表记录了哪个用户查看了哪篇文章。
用户可以在每篇文章下创建注释并查看它们。 annotation_view_history表跟踪哪个用户查看了哪个注释。
routes.rb文件中的内容是否正确?
User.rb
class User < ActiveRecord::Base
has_many :articles
has_many :viewed_articles, through: :article_view_histories
has_many :article_view_histories
has_many :annotations
has_many :viewed_annotations, through: :annotation_view_histories
has_many :annotation_view_histories
end
Annotation.rb
class Annotation < ActiveRecord::Base
belongs_to :article
belongs_to :user
has_many :annotation_view_histories
has_many :viewed_users, through: :annotation_view_histories
end
Article.rb
class Article < ActiveRecord::Base
belongs_to :user
has_many :article_view_histories
has_many :viewed_users, through: :article_view_histories
has_many :annotations
end
AnnotationViewHistory.rb
class AnnotationViewHistory < ActiveRecord::Base
belongs_to :viewed_users, :class_name => "User", :foreign_key => :user_id
belongs_to :viewed_annotations, :class_name => "Annotation", :foreign_key => :annotation_id
end
ArticleViewHistory.rb
class ArticleViewHistory < ActiveRecord::Base
belongs_to :viewed_users, :class_name => "User", :foreign_key => :user_id
belongs_to :viewed_articles, :class_name => "Article", :foreign_key => :article_id
end
的routes.rb
devise_for :users, :skip => :sessions, controllers: {registrations: 'api/registrations', passwords: 'api/passwords', confirmations: 'api/confirmations'}, defaults: { format: :json }
use_doorkeeper do
skip_controllers :applications, :authorized_applications, :authorizations
end
namespace :api, defaults: { format: :json }, path: '/' do
scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
resource :users, only: [:show] do
resource :articles do
resource :annotations
end
resource :article_view_histories
resource :annotation_view_histories
end
end
match '*path', to: -> (env) {
[400, { 'Content-Type' => 'application/json' },
[{ status: 404, error: 'Route Not Found' }.to_json]]
}, via: :all
end