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 7504995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:45:45+00:00 2026-05-29T21:45:45+00:00

I have these 3 models: class Car < ActiveRecord::Base belongs_to :user has_many :car_styles has_many

  • 0

I have these 3 models:

class Car < ActiveRecord::Base
  belongs_to :user
  has_many :car_styles
  has_many :styles, :through => :car_styles
end


class CarStyle < ActiveRecord::Base
  belongs_to :car
  belongs_to :style
end


class UserStyle < ActiveRecord::Base
  belongs_to :style
  belongs_to :user  
end


class User < ActiveRecord::Base     
  has_many :cars

  has_many :car_styles
  has_many :styles, :through => :car_styles

end

I have logged-in a user (current_user). I am trying to select all cars, where car style_id are the same like user style_id — how can I do that?

Thanks for help in advance

EDIT: schema:

cars

-id
-user_id

car_styles

-car_id
-style_id

user_styles

-user_id
-style_id

Every user has saved favorite styles – doesn’t matter how much (but approximately ±5). Every photo has added a styles as well – and again doesn’t matter how much.

And I would like to select all cars from table Cars, which have the same styles, as the current_user has added.

  • 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-05-29T21:45:47+00:00Added an answer on May 29, 2026 at 9:45 pm

    I think what you’re saying here is you have three main models:

    Car, User and Style

    CarStyle and UserStyle are joins right?

    If so, you should be able to say:

    Car.joins(:styles => [:users]).where(:users => {:id => current_user.id })
    

    If not, could you update the question with your schema?

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

Sidebar

Related Questions

if I have a ActiveRecord model: class Car < ActiveRecord::Base belongs_to :store end In,
I have these two models class Invoice < ActiveRecord::Base has_many :items accepts_nested_attributes_for :items ...
I have these models: class UserProfile(models.Model): user = models.OneToOneField(User) #etc class Organization(models.Model): users =
I have these models class User(models.Model): user_name = models.CharField() ph_number = models.CharField() class ExamPaper(models.Model):
I have a ActiveRecord Car model: class Car < ActiveRecord::Base def check_value puts self.load_size
I have a model: class Cars < ActiveRecord::Base tag_nr = rand(2007) end The Cars
I have these models: class Model1(models.Model): ... class Model2(models.Model): m1 = models.OneToOneField(Model1, related_name='m2') ...
I have these models: class Gallery(models.Model): HeadImage = models.ImageField(upload_to=gallery,blank=True,null=True) class News(Gallery): Name=models.CharField(max_length=100) #some other
Let's say I have these models: class A(models.Model): name = models.CharField(max_length=32) b = models.ForeignKey(B,
i have these two models public class Group { [Key] public int GroupID {

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.