Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:53:16+00:00 2026-06-17T11:53:16+00:00

Given this has_many, :through scenario: article.rb class Article < ActiveRecord::Base attr_accessible :body, :issue, :name,

  • 0

Given this has_many, :through scenario:

article.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

brand.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

How would I write a scope or a method or a helper to get Article records that share a Brand with the current user? Something along these lines:

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

EDIT

Something like this should work, right? I just can’t figure out the syntax.

Article.all.brands.find(1) #should find all articles available to a brand with an ID of 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:53:17+00:00Added an answer on June 17, 2026 at 11:53 am

    The solution was irritatingly simple:

    @articles = current_user.brand.articles
    

    With any luck that helps someone else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given these relationships: class Account < ActiveRecord::Base has_many :employments has_many :people, :through => :employments
Given a model like: class PhoneNumber < ActiveRecord::Base has_many :personal_phone_numbers has_many :household_phone_numbers has_many :organization_phones
class Users < ActiveRecord::Base has_many :meetings, :through => :meeting_participations has_many :meeting_participations end class Meetings
Models: class User < ActiveRecord::Base has_many :attendances has_many :courses, :through => :attendances end class
I have the following models: class Campaign < ActiveRecord::Base has_many :campaign_keywords has_many :leads, :through
Assume a standard has_many :through relationship among three models class Person < ActiveRecord::Base has_many
I have a very simple model like this: class User < ActiveRecord::Base has_many :cookies
Hi I have three tables like the following: class Workitem < ActiveRecord::Base has_many :effort
Given the following classes: class Candidate has_many :applications has_many :companies, :through => :job_offers end
Hi guys here is my code: class Tailor < ActiveRecord::Base has_many :tailor_items has_many :order_items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.