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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:05:18+00:00 2026-05-17T16:05:18+00:00

I’m working on my first Rails(3) App, and looking to seed a bunch of

  • 0

I’m working on my first Rails(3) App, and looking to seed a bunch of data.

The issue I’m having is that I want to seed some models that have a has_and_belongs_to_many relationship with other models I’ve just seeded. I’m doing what seems right, but I’m not getting the results I’m expecting.

I have an Asana model (simplified):

class Asana < ActiveRecord::Base
  has_and_belongs_to_many :therapeutic_foci
end

and the TherapeuticFocus model:

class TherapeuticFocus < ActiveRecord::Base
  has_and_belongs_to_many :asanas
end

In my db/seeds.rb, I create some TherapeuticFoci:

tf = TherapeuticFocus.create([
  {:name => 'Anxiety'},
  {:name => 'Asthma'},
  {:name => 'Fatigue'},
  {:name => 'Flat Feet'},
  {:name => 'Headache'},
  {:name => 'High Blood Pressure'},
  {:name => 'Stress'} ])

Then create an Asana:

asanaCreate = Asana.create!([
  { :english_name => 'Mountain Pose', 
    :traditional_name => 'Tadasana', 
    :pronunciation => 'TadaSANA', 
    :deck_set => 'Basic', 
    :type => 'Standing', 
    :therapeutic_foci => TherapeuticFocus.where("name in ('Stress','Flat Feet')")}
])

The result is that the TherapeuticFocus models are created, the Asana is created, but it doesn’t create the relationships to the TherapeuticFocus models. The resulting array is empty.

If I run

TherapeuticFocus.where("name in ('Stress','Flat Feet')")

in the rails console, I get the expected two records:

irb(main):010:0> TherapeuticFocus.where("name in ('Stress','Flat Feet')")
=> [#<TherapeuticFocus id: 6, name: "Flat Feet", description: nil, created_at: "2010-10-11     01:48:02", updated_at: "2010-10-11 01:48:02">, 
    #<TherapeuticFocus id: 19, name: "Stress",     description: nil, created_at: "2010-10-11 01:48:02", updated_at: "2010-10-11 01:48:02">]

So, how does one do this?

Or, is there a better way to do this?

Thanks!

POST ANSWER:

I had already added the inflection:

ActiveSupport::Inflector.inflections do |inflect|
    inflect.irregular 'focus', 'foci'
end

My migration for the join tables looks like:

create_table :asanas_therapeutic_foci, :id => false do |t|
  t.references :asana, :therapeutic_focus
end

I’ll try changing this to t.belongs_to instead of t.references and see if that works.

  • 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-17T16:05:19+00:00Added an answer on May 17, 2026 at 4:05 pm

    Did you register the pluralization for “focus”? It is not defined by default, so you will need to define it (usually in config/initializers/inflections.rb):

    ActiveSupport::Inflector.inflections do |inflect|
      inflect.irregular 'focus', 'foci'
    end
    

    You also need to make sure your migration has defined the correct join table for the HABTM association. Here is the pertinent part of the “up” migration that I used:

        create_table :asanas do |t|
          t.string :english_name
          t.string :traditional_name
          t.string :pronunciation
          t.string :deck_set
          t.string :type
        end
        create_table :therapeutic_foci do |t|
          t.string :name
        end
        create_table :asanas_therapeutic_foci, :id => false do |t|
          t.belongs_to :asana
          t.belongs_to :therapeutic_focus
        end
    

    I used the models as you quoted.

    With those bits in place, I was able to load your seed definitions.

    • 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 have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
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.