1

has_many, :throughこのシナリオを考えると:

記事.rb

class Article < ActiveRecord::Base
  attr_accessible :body, :issue, :name, :id, :brand_ids

  has_many :publications
  has_many :docs, :through => :publications
  has_many :silos
  has_many :brands, :through => :silos
end

ブランド.rb

class Brand < ActiveRecord::Base
  attr_accessible :name, :logo1, :logo2, :colour1, :colour2

  has_many :users
  has_many :prices, :dependent => :destroy
  has_many :silos
  has_many :articles, :through => :silos
end

user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
  attr_accessor :current_password
  attr_accessible :name, :password, :password_confirmation, :current_password, :email, :remember_me, :brand_id
  belongs_to :brand
end

現在のユーザーとArticle共有するレコードを取得するためのスコープ、メソッド、またはヘルパーをどのように記述すればよいでしょうか? Brandこれらの行に沿ったもの:

class Article < ActiveRecord::Base
  scope :branded_articles, lambda { |user| where('brand_ids = ?', user.brand) }
end

編集

このようなものはうまくいくはずですよね?私はちょうど構文を理解することはできません。

Article.all.brands.find(1) #should find all articles available to a brand with an ID of 1
4

1 に答える 1

1

解決策はいらいらするほど単純でした。

@articles = current_user.brand.articles

他の誰かを助ける運があれば。

于 2013-01-16T02:50:44.253 に答える