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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:07:52+00:00 2026-05-31T09:07:52+00:00

I’ve got the following structure set up in ActiveRecord in Rails 3 class Domain

  • 0

I’ve got the following structure set up in ActiveRecord in Rails 3

class Domain < AR
  has_and_belongs_to_many :videos
end

class Video < AR
  has_and_belongs_to_many :domains
  acts_as_taggable_on :tags
  scope :with_state, lambda { |s| where('state = ?', s)  }
end

Somewhere in my controller I want to find other videos from a video based on a certain tag, wich is quite easy using the find_related_tags method. But I need to restrict the related videos to the current domain which is present through a helper method called current_domain, so I came up with the following AR query:

@video = Video.find(params[:id])
@video.find_related_tags.joins(:domains).with_state(:converted).where('domains.id = ?', current_domain.id)

This AR query produces the following MySQL query:

SELECT videos.*, COUNT(tags.id) AS count FROM videos, tags, taggings
INNER JOIN `domains_videos` ON `domains_videos`.`video_id` = `videos`.`id`
INNER JOIN `domains` ON `domains`.`id` = `domains_videos`.`domain_id`
WHERE (videos.id != 5 AND videos.id = taggings.taggable_id 
  AND taggings.taggable_type = 'Video'
  AND taggings.tag_id = tags.id
  AND tags.name IN ('not'))
  AND (state = 'converted')
  AND (domains.id = 4)
GROUP BY videos.id
ORDER BY count DESC

It seems fine to me, but it produces a MySQL error:

Mysql2::Error: Unknown column 'videos.id' in 'on clause'

And that I don’t get! Why is videos.id an unkown column in

INNER JOIN `domains_videos` ON `domains_videos`.`video_id` = `videos`.`id`

All join tables are properly set up. Everything works. If I remove the .joins(:domains) part it works… I’m sure it’s something simple I’m just not seeing 🙂

  • 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-31T09:07:53+00:00Added an answer on May 31, 2026 at 9:07 am

    It seems that the MySQL query is valid, but the videos.id column is unknown cause videos is not in the join set but only used in the FROM clause. So I ended up with the solution to do the following:

    Video.find_by_sql("
      SELECT videos.*, COUNT(tags.id) AS count
      FROM videos
      INNER JOIN domains_videos ON domains_videos.video_id = videos.id
      INNER JOIN domains ON domains.id = domains_videos.domain_id
      INNER JOIN taggings ON videos.id = taggings.taggable_id AND taggings.taggable_type = '#{self.class.to_s}'
      INNER JOIN tags ON taggings.tag_id = tags.id
      WHERE tags.name IN (#{joined_tags})
      AND videos.state = 'converted'
      AND domains.id = #{domain.id}
      AND videos.id != #{id}
      GROUP BY videos.id
      ORDER BY count DESC
      LIMIT #{limit}
    ")
    

    Since everything is really joined now, the statement now is valid for MySQL.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
i got an object with contents of html markup in it, for example: string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and

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.