You didn't define many-to-many according to Rails. The correct solution is (file names should be as described):
app/models/recipe.rb
class Recipe < ActiveRecord::Base
has_many :recipe_ingredients
has_many :ingredients, :through => :recipe_ingredients
end
app/models/ingredient.rb
class Ingredient < ActiveRecord::Base
has_many :recipe_ingredients
has_many :recipes, :through => :recipe_ingredients
end
app/models/recipe_igredient.rb
class RecipeIngredient < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
end
Also verify, your join table defined, as following:
db/12345_create_recipe_ingredints.rb
class CreateRecipeIngredients < ActiveRecord::Migration
def change
create_table :recipe_ingredients, id: false do |t|
t.references :recipe
t.references :ingredient
end
add_index :recipe_ingredients, :recipe_id
add_index :recipe_ingredients, :ingredient_id
end
end
After doing this, make a test in your console:
recipe = Recipe.first # should contain data
igredient = Ingredient.first # should contain data
recipe.ingredients << ingredient
recipe.inspect
If everything works correctly and recipe.inspect contains ingredient, json should be correct