首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ActiveModel::MissingAttributeError:无法使用has_many和通过关系编写未知属性has_many

ActiveModel::MissingAttributeError:无法使用has_many和通过关系编写未知属性has_many
EN

Stack Overflow用户
提问于 2022-09-15 07:14:44
回答 1查看 95关注 0票数 0

当我意识到我缺少一些is时,模式被修改了,但是现在我不知道缺少了什么。使用Rails 7.0.3.1。

代码语言:javascript
运行
复制
create_table :properties do |t|
      t.string :code
      t.timestamp :create_date
      t.string :province
      t.string :county
      t.string :district
      t.string :area
      t.integer :rooms
      t.integer :bathrooms
      t.integer :parking
      t.boolean :pets
      t.text :address
      t.text :comments
      t.decimal :payment1
      t.decimal :payment2
      t.string :currency1
      t.string :currency2
      t.string :en_translation_key

      t.timestamps
    end
代码语言:javascript
运行
复制
create_table :included_services do |t|
      t.references :property
      t.references :service

      t.timestamps
    end
代码语言:javascript
运行
复制
create_table :included_amenities do |t|
      t.references :property
      t.references :amenity

      t.timestamps
    end

模型

代码语言:javascript
运行
复制
class Property < ApplicationRecord
    belongs_to :property_type
    belongs_to :landlord
    has_one :property_transaction
    has_many_attached :photos
    has_many :included_amenities, dependent: :destroy
    has_many :included_services, dependent: :destroy
    has_many :amenities, through: :included_amenities
    has_many :services, through: :included_services
    validates_presence_of :code, :create_date, :province, :county, :district, :area, :pets, :address, :payment1, :currency1, :property_transaction
end
代码语言:javascript
运行
复制
class IncludedAmenity < ApplicationRecord
    belongs_to :amenity
    belongs_to :property
end
代码语言:javascript
运行
复制
class IncludedService < ApplicationRecord
    belongs_to :service
    belongs_to :property
end

更新:添加控制器创建代码。

控制器

代码语言:javascript
运行
复制
def create
    tmp_params = property_params
    @property = Property.new
    tmp_params[:landlord] = Landlord.find(tmp_params[:landlord])
    tmp_params[:property_type] = PropertyType.find(tmp_params[:property_type])
    tmp_params[:property_transaction] = PropertyTransaction.find(tmp_params[:property_transaction])
    tmp_params[:services] = tmp_params[:services].reject { |service| service == '' }.map { |service| Service.find(service) }
    tmp_params[:amenities] = tmp_params[:amenities].reject { |amenity| amenity == '' }.map { |amenity| Amenity.find(amenity) }
    province_name = search_province_name(tmp_params[:province])[tmp_params[:province]]
    county_name = search_county_name(tmp_params[:province], tmp_params[:county])[tmp_params[:county]]
    tmp_params[:district] = search_district_name(tmp_params[:province], tmp_params[:county], tmp_params[:district])[tmp_params[:district]]
    tmp_params[:province] = province_name
    tmp_params[:county] = county_name
    @property.attributes = tmp_params

    respond_to do |format|
      if @property.save
        format.html { redirect_to @property, notice: "Property was created successfully." }
        format.json { render :show, status: :created, location: @property }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @property.errors, status: :unprocessable_entity }
      end
    end
  end

def property_params
      params.require(:property).permit(:code, :create_date, :property_transaction, :province, :county, :district, :area, :rooms,
        :bathrooms, :parking, :payment1, :payment2, :currency1, :currency2,
        :pets, :landlord, :address, :comments, :property_type, services: [], amenities: [], photos: [])
    end

试图创建完整属性时出错的是:

代码语言:javascript
运行
复制
ActiveModel::MissingAttributeError (can't write unknown attribute `property_id`

          raise ActiveModel::MissingAttributeError, "can't write unknown attribute `#{name}`"
          ^^^^^):

想不出有什么其他地方可以尝试寻找property_id属性。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-16 06:19:54

在清理控制器代码并逐一分配每个属性之后,我发现罪魁祸首似乎是这一行:

代码语言:javascript
运行
复制
@property.property_transaction = PropertyTransaction.find(property_params[:property_transaction])

所以我再看一遍这段关系,很明显:

代码语言:javascript
运行
复制
class Property < ApplicationRecord
    belongs_to :property_type
    belongs_to :landlord
    has_one :property_transaction
代码语言:javascript
运行
复制
class PropertyTransaction < ApplicationRecord
    has_many :properties
    validates_presence_of :name, :en_translation_key
    validates_uniqueness_of :name, :en_translation_key
end

没有孩子的关系,所以解决办法就是这样:

代码语言:javascript
运行
复制
class Property < ApplicationRecord
    belongs_to :property_type
    belongs_to :landlord
    belongs_to :property_transaction

迁移实际上是正确的,所以这就是我所需要的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73726971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档