Rails many to many relations with has many through relationships

huangapple go评论52阅读模式
英文:

Rails many to many relations with has many through relationships

问题

我有以下使用has_many :through关联的模型。

一个食谱可以有多个季节。
一个季节可以有多个食谱。

食谱

class Recipe < ApplicationRecord
  attribute :name

  has_many :recipe_seasons
  has_many :seasons, through: :recipe_seasons
end

季节

class Season < ApplicationRecord
  has_many :recipe_seasons
  has_many :recipes, through: :recipe_seasons
end

食谱季节

class RecipeSeason < ApplicationRecord
  belongs_to :recipe
  belongs_to :season
end

我目前在索引页面上显示所有的食谱,使用以下代码:

控制器

def index
  @recipes = Recipe.all
  render index: @recipes, include: [:recipe_seasons, :seasons]
end

视图

<% if @recipes.present? %>
  <% @recipes.each do |recipe| %>
    <%= link_to recipe.name, [recipe] %>
  <% end %>
<% end %>

我想要的是在每个食谱旁边显示季节。一个食谱可以有多个季节,所以我在现有的食谱循环内添加了另一个循环。

我尝试过以下代码:

<% @recipes.each do |recipe| %>
  <% recipe.seasons.each do |season| %>
    <%= link_to recipe.name, [recipe] %>
    <%= season.name %>
  <% end %>
<% end %>

当前行为

食谱1 - 季节1

食谱1 - 季节2

期望的行为

食谱1 - 季节1,季节2

食谱2 - 季节4

英文:

I have the following models using the has_many :through relationship.

A recipe can have many seasons.
A season can have many recipes.

Recipe

class Recipe &lt; ApplicationRecord
  attribute :name

  has_many :recipe_seasons
  has_many :seasons, through: :recipe_seasons
end

Season

class Season &lt; ApplicationRecord
    has_many :recipe_seasons
    has_many :recipes, through: :recipe_seasons
end

Recipe Season

class RecipeSeason &lt; ApplicationRecord
  belongs_to :recipe
  belongs_to :season
end

I'm currently displaying all recipes on the index page using the the following

Controller

  def index
    @recipes = Recipe.all
    render index: @recipes, include: [:recipe_seasons, :seasons]
  end

View

 &lt;% if @recipes.present? %&gt;
   &lt;% @recipes.each do |recipe| %&gt;
     &lt;%= link_to recipe.name,[recipe] %&gt;
 &lt;% end %&gt;

What I want to do is to have the seasons displayed with the each recipe. A recipe can have more than one season and so I added another for loop inside the existing one for recipes.

I have so far tried:

&lt;% @recipes.each do |recipe| %&gt;
  &lt;% recipe.seasons.each do |season| %&gt;
    &lt;%= link_to recipe.name,[recipe] %&gt;
      &lt;%= season.name %&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
&lt;% end %&gt;

Current Behaviour

Recipe 1 - Season 1

Recipe 1 - Season 2

Expected Behaviour

Recipe 1 - Season 1, Season 2

Recipe 2 - Season 4

答案1

得分: 1

你必须在link_tobody参数中包含季节(链接中显示的文本)。

<% @recipes.each  do |recipe| %>
   <%= link_to "#{recipe.name} - #{recipe.seasons.map(&:name).join(', ')}", [recipe] %>
<% end  %>
英文:

You must include the seasons in the body parameter of the link_to (the text displayed in the link)

&lt;% @recipes.each do |recipe| %&gt;
   &lt;%= link_to &quot;#{recipe.name} - #{recipe.seasons.map(&amp;:name).join(&#39;, &#39;)}&quot;, [recipe] %&gt;
&lt;% end %&gt;

huangapple
  • 本文由 发表于 2023年1月8日 22:50:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定