本文介绍了没有这样的列,但列存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多对多的关系的问题。我已经建立了关系,通过第三方叫rtimespans。

I have a problem with a many to many relation. I've set up the relation via a third party called rtimespans.

就像在教程告知:

的时间跨度模型:

class Timespan < ActiveRecord::Base

  validates :name, presence: true
  has_many :rtimespans
  has_many :subgroups, through: :rtimespans

  validates :start_h, presence: true
  validates :start_m, presence: true
  validates :end_h,   presence: true
  validates :end_m,   presence: true

  validate :e_must_be_0_60
  validate :e_must_be_0_24
  validate :s_must_be_0_60
  validate :s_must_be_0_24
  validate :e_bigger_s


    def e_must_be_0_60

        errors.add(:end_m,"must be 0-60") unless 0 < end_m.to_i
        errors.add(:end_m,"must be 0-60") unless 60 > end_m.to_i

    end

    def e_must_be_0_24

        errors.add(:end_h,"must be 0-24") unless 0 < end_h.to_i
        errors.add(:end_h,"must be 0-24") unless 24 > end_h.to_i

    end

    def s_must_be_0_60

        errors.add(:start_m,"must be 0-60") unless 0 < start_m.to_i
        errors.add(:start_m,"must be 0-60") unless start_m.to_i < 60

    end

    def s_must_be_0_24

        errors.add(:start_h,"must be 0-24") unless 0 < start_h.to_i
        errors.add(:start_h,"must be 0-24") unless start_h.to_i < 24

    end

    def e_bigger_s

        s=start_h.to_i*60+start_m.to_i
        e=end_h.to_i*60+end_m.to_i

        errors.add(:end_h,"End must be bigger than start") unless e > s

    end

end

该rtimespan模型:

The rtimespan-model:

    class Rtimespan < ActiveRecord::Base
      belongs_to :timespan
      belongs_to :subgroup
      validates  :subgroup, presence: true
      validates  :subgroup, presence: true
    end

和亚组模型:

    class Subgroup < ActiveRecord::Base
  belongs_to :group

  has_many :timespans
  has_many :memberships
  has_many :users, through: :memberships

  has_many :translations
  has_many :actioncodes, through: :translations

  has_many :entries

  has_many :rules

  validates :name, presence: true, uniqueness: true
  validates :group_id, presence: true

   has_many :rtimespans
   has_many :timespans, through: :rtimespans
end

不管怎么说,当我想打电话给过东西的关系,我得到这个错误。

Anyways, when I want to call something over the relationship, I get this Error.

ActiveRecord::StatementInvalid in Subgroups#show

Showing C:/xampp/htdocs/fluxcapacitor/app/views/subgroups/show.html.erb where line #40 raised:

SQLite3::SQLException: no such column: rtimespans.subgroup_id: SELECT "timespans".* FROM "timespans" INNER JOIN "rtimespans" ON "timespans"."id" = "rtimespans"."timespan_id" WHERE "rtimespans"."subgroup_id" = ?

谁能告诉我,如何解决这一问题,或至少告诉我,在这里这个错误来自哪里?

Can anyone tell me, how to fix this, or at least tell me, where this error comes from?

推荐答案

发现我的错误!我有一个双号

Found my Error! I have a "double ID":

  create_table "rtimespans", force: true do |t|
    t.integer "subgroup_id_id"
    t.integer "timespan_id_id"
  end

我创建了一个错误的迁移:

I created a wrong migration:

class CreateRtimespans < ActiveRecord::Migration
  def change
        create_table :rtimespans do |t|
            t.belongs_to :subgroup_id
            t.belongs_to :timespan_id
        end
    end
end

这应该是:

class CreateRtimespans < ActiveRecord::Migration
  def change
        create_table :rtimespans do |t|
            t.belongs_to :subgroup
            t.belongs_to :timespan
        end
    end
end

这篇关于没有这样的列,但列存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:52