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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:11:24+00:00 2026-05-26T16:11:24+00:00

I’ve got a Rails app that’ll be sitting on top of a legacy database

  • 0

I’ve got a Rails app that’ll be sitting on top of a legacy database with some ugly tables that I’m having to deal with. One is a feature_attributes table related to features. Problem is that this feature_attributes table doesn’t have a primary key. I wouldn’t think that’d be a problem, but evidently it is. I’ve got my model name which is different from the table name, but I’m using set_table_name to specify the right one.

class Feature < ActiveRecord::Base
  has_many :feature_attributes
end

class FeatureAttribute < ActiveRecord::Base
  set_table_name 'feature_attribute'
  belongs_to :feature
end

Once I load a feature that I know has related feature_attributes and call feature.feature_attributes on it, I get nil. Matter of fact, even FeatureAttribute.first gives me nil. The result of FeatureAttribute.any? returns false. I’m worried that ActiveRecord isn’t reading any of the data from the table because there isn’t a primary key. Is that what’s going on here?

The feature_attribute table has the following columns.

feature_id
attribute_name
attribute_value
created_date
modified_date

Help!

I’ll also note that running the generated SQL directly against the MySQL server actually gets me the rows I want.

SELECT `feature_attribute`.* FROM `feature_attribute` WHERE `feature_attribute`.`feature_id` = 24;

EDIT: I am so sorry. Next time I’ll learn to check my database.yml. Evidently I was reading from a test database that had an identical feature table, but the feature_attribute table was totally empty.

I feel like a moron. Thanks for your help, everyone; I’m up voting you all for your troubles. I did like just about everybody’s answer. (Can I down vote myself? :))

  • 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-26T16:11:24+00:00Added an answer on May 26, 2026 at 4:11 pm

    Try to also set the primary key:

    class FeatureAttribute < ActiveRecord::Base
      set_table_name 'feature_attribute'
      set_primary_key 'feature_id'
    
      belongs_to :feature
    end
    

    UPDATE

    I think your problem lies anywhere else. I just tested and ActiveRecords works fine with tables without a primary key:

    For a simple table:

    class CreateThings < ActiveRecord::Migration
      def change
        create_table :things, :id => false do |t|
          t.string :name
    
          t.timestamps
        end
      end
    end
    

    in console:

    Loading development environment (Rails 3.1.1)
    irb(main):001:0> Thing.create(:name=>'A name for the thing')
       (0.1ms)  BEGIN
      SQL (0.3ms)  INSERT INTO `things` (`created_at`, `name`, `updated_at`) VALUES ('2011-11-02 16:33:48', 'A name for the thing', '2011-11-02 16:33:48')
       (40.3ms)  COMMIT
    => #<Thing name: "A name for the thing", created_at: "2011-11-02 16:33:48", updated_at: "2011-11-02 16:33:48">
    irb(main):002:0> Thing.first
      Thing Load (0.7ms)  SELECT `things`.* FROM `things` LIMIT 1
    => #<Thing name: "A name for the thing", created_at: "2011-11-02 16:33:48", updated_at: "2011-11-02 16:33:48">
    irb(main):003:0> 
    

    UPDATE 2
    Not very fine:

    irb(main):003:0> Thing.create(:name=>'Another thing')
       (0.2ms)  BEGIN
      SQL (0.4ms)  INSERT INTO `things` (`created_at`, `name`, `updated_at`) VALUES ('2011-11-02 16:40:59', 'Another thing', '2011-11-02 16:40:59')
       (35.4ms)  COMMIT
    => #<Thing name: "Another thing", created_at: "2011-11-02 16:40:59", updated_at: "2011-11-02 16:40:59">
    irb(main):004:0> Thing.first
      Thing Load (0.5ms)  SELECT `things`.* FROM `things` LIMIT 1
    => #<Thing name: "A name for the thing", created_at: "2011-11-02 16:33:48", updated_at: "2011-11-02 16:33:48">
    irb(main):005:0> Thing.last
      Thing Load (11.8ms)  SELECT `things`.* FROM `things` ORDER BY `things`.`` DESC LIMIT 1
    Mysql2::Error: Unknown column 'things.' in 'order clause': SELECT  `things`.* FROM `things`  ORDER BY `things`.`` DESC LIMIT 1
    ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'things.' in 'order clause': SELECT  `things`.* FROM `things`  ORDER BY `things`.`` DESC LIMIT 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
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'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.