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

  • Home
  • SEARCH
  • 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 7406663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:35:32+00:00 2026-05-29T05:35:32+00:00

I have these model classes: class Song < ActiveRecord::Base has_many :urls, as: :media has_one

  • 0

I have these model classes:

class Song < ActiveRecord::Base  
  has_many :urls, as: :media
  has_one :url, as: :media
end

class Url < ActiveRecord::Base
  belongs_to :media, polymorphic: true
end

The strange thing here is that Song.first.url always returns nil, but Song.first.urls returns a list of urls.

Here is the generated SQL queries:

Song.includes(:url).first.url # => nil

Song Load (0.4ms)  SELECT `songs`.* FROM `songs` LIMIT 1
Url Load (0.4ms)  SELECT `urls`.* FROM `urls` WHERE `urls`.`media_type` = 'Song' AND `urls`.`media_id` IN (1)

Song.includes(:urls).first.urls # => [list of urls]

Song Load (0.7ms)  SELECT `songs`.* FROM `songs` LIMIT 1
Url Load (0.5ms)  SELECT `urls`.* FROM `urls` WHERE `urls`.`media_type` = 'Song' AND `urls`.`media_id` IN (1)

I’m running rails 3.2.0.rc2 on Ruby 1.9.2

Any ideas why this is happening?

  • 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-29T05:35:32+00:00Added an answer on May 29, 2026 at 5:35 am

    This was my fault, I had accidently implemented my own Song#url method. It took me a while to find it.

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

Sidebar

Related Questions

I have the following model classes declared by SQLAlchemy: class User(Base): id = Column(Integer,
Here's an example: If I have these classes class Author(models.Model): name = models.CharField(max_length=45) class
I have these 3 models in models.py class Customer(models.Model): name = models.CharField(max_length=50) .... class
I have these classes: public class Product { [Key] public virtual int ProductId {
The scenario Suppose I have the following two model classes: public class ProductColor {
I want to generate a list using my django model say I have these
Background: I have an application with athletes, each of these athletes (Athlete model) can
I have some model objects I'm using in my Java client application. Later these
I have a rails model that validates uniqueness of 2 form values. If these
I have an ActiveRecord model whose fields mostly come from the database. There are

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.