I am having an issue updating a has_many through record. Here is my setup:
class TastingGroup < ActiveRecord::Base
has_many :group_wine
has_many :wines, through: :group_wine
end
class GroupWine < ActiveRecord::Base
belongs_to :tasting_group
belongs_to :wine
end
class Wine < ActiveRecord::Base
has_many :group_wine
has_many :tasting_groups, through: :group_wine
end
I was trying to use the acts_as_list for this, because the order of the wines in a TastinGroup matter, so I have added a 'position' attribute to the GroupWine model.
However, when I try to even update a GroupWine record, I get the following error, and here is what I am doing.
gw = GroupWine.first
#<GroupWine:0x007fd9f7c38b50> {
:wine_id => 1,
:tasting_group_id => 1,
:position => nil
}
gw.position = 1
gw.save
And here is the error I get...
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'group_wines.' in 'where clause': UPDATE `group_wines` SET `position` = 3 WHERE `group_wines`.`` IS NULL
What is up with the NULL for group_wines, and why is it adding that where clause?
Thanks.
Try pluralizing the group_wine object to group_wines
class TastingGroup < ActiveRecord::Base
has_many :group_wines
has_many :wines, through: :group_wines
end
class GroupWine < ActiveRecord::Base
belongs_to :tasting_group
belongs_to :wine
end
class Wine < ActiveRecord::Base
has_many :group_wines
has_many :tasting_groups, through: :group_wines
end
Related
I'm trying to create a has_many :through association with a custom scope. The problem is that the scope has a joins with a 3rd model, and that seems to be breaking the association.
These are the models:
class Student < ApplicationRecord
has_many :lesson_applications, through: :lesson_requests
has_many :lessons, through: :lesson_applications
has_many :scoped_lessons, -> { custom_scope }, through: :lesson_applications, source: :lesson
has_many :tutors, through: :scoped_lessons # Broken association
end
class Lesson < ApplicationRecord
belongs_to :lesson_application
has_one :tutor, through: :lesson_application
has_one :time_range, as: :time_rangeable
scope :custom_scope, (lambda {
joins(:time_range).where('time_ranges.attribute BETWEEN ? AND ?', value1, value2)
})
end
class LessonApplication < ApplicationRecord
belongs_to :lesson_request
belongs_to :tutor
has_one :lesson
end
class Tutor < ApplicationRecord
has_many :lesson_applications
has_many :lessons, through: lesson_applications
end
class TimeRange < ApplicationRecord
belongs_to :time_rangeable, polymorphic: true
end
I expected to get the student's tutors from the scoped lessons, but instead I get an missing FROM-clause entry for table 'time_ranges'. Which is clearly and error that can be seen on the generated query.
The generated query when I do student.tutors is:
SELECT "tutors".* FROM "tutors" INNER JOIN "lesson_applications" ON "tutors"."id" = "lesson_applications"."tutor_id" INNER JOIN "lessons" ON "lesson_applications"."id" = "lessons"."lesson_application_id" INNER JOIN "lesson_applications" "lesson_applications_tutors" ON "lessons"."lesson_application_id" = "lesson_applications_tutors"."id" INNER JOIN "lesson_requests" ON "lesson_applications_tutors"."lesson_request_id" = "lesson_requests"."id" WHERE "lesson_requests"."student_id" = $1 AND (time_ranges.start BETWEEN '2018-04-11 20:27:34.798992' AND '2018-04-11 20:27:34.799090') LIMIT $2
The method I'm using temporarily instead of the association is:
class Student < ApplicationRecord
def tutors
scoped_lessons.map(&:tutor).uniq
end
end
I am using ruby 2.5.1 with Rails 5.1.6
Thank you in advance.
Edit: some associations were wrong.
Hi I'm trying to set up a many to many relationship in my app. I have two models Count.rb
class Count < ApplicationRecord
has_many :users, through: :counts_users
end
users.rb:
class User < ApplicationRecord
has_many :counts, through: :counts_users
end
and counts_users.rb:
class CountsUser < ApplicationRecord
belongs_to :user
belongs_to :count
end
Now I can create a count
Count.new(message: 'hello')
but if I then do
Count.last.users << User.last
I get the error ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :counts_users in model ErrorCount
I assume I've done something wrong setting up the association, but I'm not sure what?
Your models' associations should be set up like this:
# count.rb
class Count < ApplicationRecord
has_many :counts_users
has_many :users, through: :counts_users
end
# user.rb
class User < ApplicationRecord
has_many :counts_users
has_many :counts, through: :counts_users
end
# counts_user.rb
class CountsUser < ApplicationRecord
belongs_to :user
belongs_to :count
end
See: the Rails Guides on has_many :through Association
Problem: In enrollment.attendances.count(:id) Enrollment must have access only to those attendances, that match it by both student_id and course_id.
Here are my 4 db dependancies:
class Enrollment < ActiveRecord::Base
belongs_to :course
belongs_to :student
has_many :attendances, through: :client
has_many :attendances, through: :course
,
class Student < ActiveRecord::Base
has_many :enrollments
has_many :attendances
,
class Course < ActiveRecord::Base
has_many :enrollments
has_many :attendances
,
class Attendance < ActiveRecord::Base
belongs_to :student
belongs_to :course
,
The schema of the model dependancies:
You can first find the object from Enrollment by student_id and course_id
and then check whether it attended or not
then you could find the count.
#enrolls = Enrollment.where(course_id: id1, student_id: id2)
#enrolls.each do |enrol|
if enrol.attended == true
puts Attendance.where(course_id: id1, student_id: id2).count
end
end
As mentioned here
putting the following line in enrollment.rb
has_many :attendances, ->(obj) { where("#{Attendance.quoted_table_name}.client_id = ?", obj.client_id)}, through: :course
perfectly solves the question
class Card < ApplicationRecord
has_one :card_rating
has_one :rating, through: :card_rating
end
class Rating < ApplicationRecord
has_many :card_ratings
has_many :cards, through: :card_ratings
end
class CardRating < ApplicationRecord
belongs_to :card
belongs_to :rating
end
I want to do something along the lines of the following:
c = card.card_rating.new
c << rating
But there doesn't seem to be any association going on at all, because already at the first statement I receive the following error:
undefined method `new' for nil:NilClass
You do not need a join table for a one to many relation:
class Card
belongs_to :rating
end
class Rating
has_many :cards
end
Rather you would use has_one :through in case the domain dictates that the relation is indirect.
class Student
belongs_to :school
has_one :headmaster, through: :school
end
class School
has_many :students
end
class Headmaster
belongs_to :school
has_many :students, through: :school
end
I have this 3 simple models:
class Conversation < ActiveRecord::Base
belongs_to :user_conversation
has_many :users, through: :user_conversation
end
class UserConversation < ActiveRecord::Base
belongs_to :user
belongs_to :conversation
end
class User < ActiveRecord::Base
belong_to :user_conversation
has_many :conversations, through: :user_conversation
end
I want to insert some data in UserConversation by:
Conversation.find(params[:some_id]).users << User.where(id: [1,2,3])
The idea is append existing users to existing Conversation.
When i do this by above command I get error:
ActiveModel::MissingAttributeError: can't write unknown attribute `user_conversation_id'
I suppose I have error in associations?