我的模型关系没有按预期工作

时间:2023-02-08 13:19:35

I've got two models that have a certain relationship to each other.

我有两个模型彼此有一定的关系。

I have a meal model and a *meal_food* model.

我有一个餐模型和* meal_food *模型。

Here they are as I have them written at the moment:

在这里他们就像我现在写的一样:

class MealFood < Food
  has_and_belongs_to_many :meal
end  

class Meal < ActiveRecord::Base
  belongs_to :user
  has_many :meal_food

  attr_accessible :user_id
end   

Whenever I attempt to create a meal_food for a meal like so:

每当我尝试为这顿饭创造一顿饭食物时:

 meal.meal_food.create()

I get this error:

我收到此错误:

ActiveRecord::UnknownAttributeError: unknown attribute: meal_id

If I pass in something like this:

如果我传入这样的东西:

meal.meal_food.create(meal_id:meal.id)

I get this error:

我收到此错误:

ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: meal_id

What I expect is to have a meal_food created and have it associated with the meal it was created for. Although, it can have many meals. So I would expect that if I query meal.meal_food it would return all the foods which are associated with that meal. The fact that rails is asking me for a meal_id makes me think that rails thinks this is a one-to-one relationship and the relationship table isn't being created.

我期望的是创造一顿饭食物并将其与它所创造的膳食联系起来。虽然,它可以有很多餐。所以我希望如果我查询meal.meal_food,它会返回与该餐有关的所有食物。 rails问我for meal_id的事实让我觉得rails认为这是一对一的关系而且关系表没有创建。


I've created an Join table. Here's the schema for it:

我创建了一个Join表。这是它的架构:

create_table "meals_meal_food", :force => true do |t|
   t.integer "meal_id"
   t.integer "meal_food_id"
end  

I've also changed my meal model to be like so:

我也改变了我的用餐模式:

class Meal < ActiveRecord::Base
  belongs_to :user
  has_and_belongs_to_many :meal_foods

 attr_accessible :user_id
end     

Error now is:

现在错误是:

meal.meal_food.create()

>> NoMethodError: undefined method `meal_food' for #<Meal id: 1, user_id: 1>

2 个解决方案

#1


1  

As far as I know your has_and_belongs_to_many association is wrong, you need to have it in both models:

据我所知你的has_and_belongs_to_many关联是错误的,你需要在两个模型中都有它:

class MealFood < Food
  has_and_belongs_to_many :meals
end  

class Meal < ActiveRecord::Base
  has_and_belongs_to_many :meal_foods
end

Also, the model needs to be pluralized, have a look at this guide.

此外,模型需要复数,看看本指南。

You would then need to create a meal_foods_meals join table with meal_id and meal_food_id integer columns.

然后,您需要使用meal_id和meal_food_id整数列创建一个meal_foods_meals连接表。

Then to access your associations you must use the plural form:

然后要访问您的关联,您必须使用复数形式:

meal.meal_foods.create

If you want use has_many / belongs_to insead you would still need to pluralize the has_many

如果你想使用has_many / belongs_to insead,你仍然需要复数has_many

class MealFood < Food
  belongs_to :meal
end  

class Meal < ActiveRecord::Base
  has_many :meal_foods
end

#2


-1  

Just change your model to this:

只需将您的模型更改为:

class Meal < ActiveRecord::Base
  belongs_to :user
  has_many :meal_food

  attr_accessible :user_id, :meal_id
end

Note: exact duplicate question here.

注意:这里完全重复的问题。

#1


1  

As far as I know your has_and_belongs_to_many association is wrong, you need to have it in both models:

据我所知你的has_and_belongs_to_many关联是错误的,你需要在两个模型中都有它:

class MealFood < Food
  has_and_belongs_to_many :meals
end  

class Meal < ActiveRecord::Base
  has_and_belongs_to_many :meal_foods
end

Also, the model needs to be pluralized, have a look at this guide.

此外,模型需要复数,看看本指南。

You would then need to create a meal_foods_meals join table with meal_id and meal_food_id integer columns.

然后,您需要使用meal_id和meal_food_id整数列创建一个meal_foods_meals连接表。

Then to access your associations you must use the plural form:

然后要访问您的关联,您必须使用复数形式:

meal.meal_foods.create

If you want use has_many / belongs_to insead you would still need to pluralize the has_many

如果你想使用has_many / belongs_to insead,你仍然需要复数has_many

class MealFood < Food
  belongs_to :meal
end  

class Meal < ActiveRecord::Base
  has_many :meal_foods
end

#2


-1  

Just change your model to this:

只需将您的模型更改为:

class Meal < ActiveRecord::Base
  belongs_to :user
  has_many :meal_food

  attr_accessible :user_id, :meal_id
end

Note: exact duplicate question here.

注意:这里完全重复的问题。