Reputation: 3500
I have a problem with a many to many relation. I've set up the relation via a third party called rtimespans.
Just like in the tutorials told:
The timespan-model:
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
The rtimespan-model:
class Rtimespan < ActiveRecord::Base
belongs_to :timespan
belongs_to :subgroup
validates :subgroup, presence: true
validates :subgroup, presence: true
end
And the subgroup-model:
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?
Upvotes: 0
Views: 129
Reputation: 3500
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
It should have been:
class CreateRtimespans < ActiveRecord::Migration
def change
create_table :rtimespans do |t|
t.belongs_to :subgroup
t.belongs_to :timespan
end
end
end
Upvotes: 0
Reputation: 1097
I would check migrations as the error indicates. To find out if there's a missing column, please check your schema.rb to make sure the column exist
Upvotes: 2
Reputation: 33542
I Suppose the error is due to you mentioned relation to timespans two times in your Subgroup
model.
You have defined
has_many :timespans and
has_many :timespans, through: :rtimespans
Please correct it in the model and check.
Upvotes: 0