gpt4 book ai didi

ruby-on-rails - has_many :through Association, NoMethodError( `each' 的未定义方法 "":String)并保存关联

转载 作者:太空宇宙 更新时间:2023-11-03 16:17:57 24 4
gpt4 key购买 nike

我无法让 has_many :through Association 工作。我是否需要在 post_controller 的创建方法中设置一些额外的东西,以保存 post_category? (在代码下方,错误和我的尝试)

我有一个 has_many :through Association with post, post_category and as join 表分类如下:

帖子模型:

class Post < ApplicationRecord
has_many :categorizations
has_many :post_categories, :through => :categorizations
end

Post::Category 模型(使用命名空间以获得更好的项目结构):

class Post::Category < ApplicationRecord
has_many :categorizations
has_many :posts, :through => :categorizations
end

分类模型(使用类名,Post::Category 命名空间的原因):

class Categorization < ApplicationRecord
belongs_to :post
belongs_to :post_category, :class_name => 'Post::Category'
end

然后我在 View 中有一个选择,我在其中创建一个带有 post_category 的帖子:

<div class="form-group">
<%= f.label :post_categories, :class => 'control-label', :value => 'Category: ' %>
<%= f.select :post_categories, options_from_collection_for_select(all_post_categories, :id, :name), {}, {class: 'selectpicker', :'data-live-search' => 'true', required: 'false' } %>
</div>

在 posts_controller 中,我允许使用 :post_categories 符号:

def post_params
params.require(:post).permit(:post_categories)
end

这是我的协会迁移文件:

class CreateCategorizations < ActiveRecord::Migration[5.0]
def change
create_table :categorizations do |t|
t.integer :post_id
t.integer :post_category_id

t.timestamps
end
add_index :categorizations, :post_id
add_index :categorizations, :post_category_id
# multiple-key index enforces uniqueness on (post_id, post_category_id)
# pairs, so that a category can't have the same post twice
add_index :categorizations, [:post_id, :post_category_id], unique: true
end
end

当我尝试提交表单时,我在日志中收到以下错误:

NoMethodError (undefined method `each' for "3":String):
app/controllers/posts_controller.rb:32:in `create'

所以我搜索了一些答案 ok -> You can't assign string to association .

我尝试了另一种方法让协会继续运作。

所以我尝试使用 post_category_id,就像推荐的一样 here

我在view的select里设置

<%= f.select :post_category_id, options_from_collection_for_select(all_post_categories, :id, :name), {}, {class: 'selectpicker', :'data-live-search' => 'true', required: 'false' } %>

然后在 posts_controller 中允许 category_id:

def post_params
params.require(:post).permit(:post_category_id)
end

在 Post 模型中:

class Post < ApplicationRecord
has_many :categorizations
has_many :post_category_id, :through => :categorizations
end

这是新的错误:

Could not find the source association(s) "post_category_id" or :post_category_id in model Categorization. Try 'has_many :post_category_id, :through => :categorizations, :source => <name>'. Is it one of post or post_category?

好的,因为没有 post_category_id 关联。

更新:

post_params:

<ActionController::Parameters {"content"=>"content", "post_categories"=>"2"} permitted: true>

最佳答案

我用这个 answer 找到了适合我的解决方案还有这个question .

第一个只保存一个 Post::Category with Post 的解决方案

关于这个answer ,我必须将 posts_controller 中的 permit 参数更改为:

def post_params
params.require(:post).permit(:post_category_ids)
end

然后我还需要在创建带有 post_category 的帖子的 View 中更改选择:

<div class="form-group">
<%= f.label :post_category_ids, :class => 'control-label', :value => 'Category: ' %>
<%= f.select :post_category_ids, options_from_collection_for_select(all_post_categories, :id, :name), {}, {class: 'selectpicker', :'data-live-search' => 'true', required: 'false' } %>
</div>

就是这样。其余代码仍然与我在第二次尝试我的问题之前展示的相同。

用一个帖子保存多个 Post::Category 的第二种解决方案

关于这个question ,我需要将 posts_controller 中的 permit 参数更改为:

def post_params
params.require(:post).permit(:post_category_ids => [])
end

要同时选择多个 Post::Category,我需要将 multiple: 'true' 添加到 View 中的选择中:

<div class="form-group">
<%= f.label :post_category_ids, :class => 'control-label', :value => 'Category: ' %>
<%= f.select :post_category_ids, options_from_collection_for_select(all_post_categories, :id, :name), {}, {class: 'selectpicker', :'data-live-search' => 'true', required: 'false', multiple: 'true' } %>
</div>

关于ruby-on-rails - has_many :through Association, NoMethodError( `each' 的未定义方法 "":String)并保存关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39603168/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com