I'm working on my own Web-app but I'm facing a problem.
Here is my models :
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :player_seasons
has_many :forecasts, through: :player_seasons
end
class PlayerSeason < ApplicationRecord
belongs_to :user
belongs_to :season
belongs_to :championship
has_many :forecasts
has_many :matches, through: :forecasts
end
class Championship < ApplicationRecord
belongs_to :season
has_many :player_seasons
has_many :users, through: :player_seasons
end
#playerseasons = PlayerSeason.all
I am iterating on: #playerseasons
I would like to compare all the PlayerSeason (#playerseasons) as following :
#playerseasons.each do |playerseason|
if playerseason.championship_id == #playerseason.championship
else
end
end
The idea is comparing all the playerseasons, if a playerseason has the same championship as the current_user, something will appears otherwise something else (basic condition).
But I'm stuck and can't access the exact championship of the current user, on the other side I can access the championship of the playerseasons.
Thank you by advance :)
I did not look critically at database design model which potentially could be done differently. Also I'm taking this as a non-production app as you mention in your question therefore database load optimization is a second priority.
With what you have now I would do something like just to get it to work
user_championships = current_user.player_seasons.map(&:championship).uniq
#playerseasons.each do |playerseason|
if user_championships.include?(playerseason.championship)
else
end
end
Related
The goal is to hide a match on the front side if the user has already forecasted the match.
I'm showing all the matches if they have not started yet and I would like to show it or not if the user has already forecasted on it.
For this I'm using :
#matches.each do |match|
if Time.parse(match.kick_off) > Time.now && ??
But then when I use current_user.forecasts, I access all the forecasts of the user but then I can't compare it with the match and see if the forecast exists for a special match.
class Match < ApplicationRecord
has_many :forecasts
has_many :season_matchs
end
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :player_seasons
has_many :forecasts, through: :player_seasons
end
class Forecast < ApplicationRecord
belongs_to :player_season
belongs_to :match
end
I finally found the answer to my question by myself.
For those who are interested, I used .find_by
current_user.forecasts.find_by(match_id: match.id).nil?
I have a Course and Lesson models. Course has several lessons. I want to find all the lessons for currently logged in student to generate kind of timetable.
I have a method that returns all the courses that this student is studying. Now I want to get all lessons from all those courses in #courses into #lessons, something like:
def index
#courses = current_student.find_courses
#lessons = #courses.lessons
end
Is it possible to do it somehow simple on one line?
The find_courses method is implemented as following:
def find_courses
Course.where("id IN (?)", StudentAssignment.select("course_id").where('student_id == (?)', self.id))
end
The Models:
class Student < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :student_assignments
has_many :courses, :through => :student_assignments
....
class Lesson < ApplicationRecord
belongs_to :room
belongs_to :teacher
belongs_to :course
....
class Course < ApplicationRecord
has_many :lessons, dependent: :destroy
has_many :teacher_assignments
has_many :teachers, :through => :teacher_assignments
has_many :student_assignments
has_many :students, :through => :student_assignments
...
class Student < ApplicationRecord
has_many :courses
def active_lessions
Lession.joins(course: :students).where(students: {id: self.id})
end
end
In this way you can directly get all active lesssions for current_user
current_student.active_lessions
Try:
#lessons = #courses.flat_map(&:lessons)
It takes each course in #courses list and gets the list of lessons for that course.
I want to make audits for nested associations like I have
User has_many addresses i.e either
HomeAddress or OfficeAddress
Now if I have just one table Address and I have used type and id for differentiating them.In this case if I use associated_audits for User then it will make just one audit record and whenever I update the record again ,its just replacing the previous audit with the last one.
Here is the models association:
class Patient < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable,
:trackable, :validatable, :confirmable, request_keys: [:subdomain]
has_one :home_address,-> { where(addr_type: 'home') },class_name: 'Address'
has_one :office_address,-> { where(addr_type: 'office') }, class_name: 'Address'
has_associated_audits
accepts_nested_attributes_for :home_address, allow_destroy: true
accepts_nested_attributes_for :office_address, allow_destroy: true
end
class Address < ActiveRecord::Base
belongs_to :patient
audited
end
class Address < ActiveRecord::Base
belongs_to :patient
audited associated_with: :patient
end
class Patient < ActiveRecord::Base
has_many :addresses
has_associated_audits
end
I'm working on a project where I will need to test users at the end of a section. Using a Nested Model Form I'd like for users to be able to select answers and have those stored. I'm trying to build it out for myself to improve and could use advice from more experienced developers on how to best approach this.
I am assuming that this is many-to-many through relationship and I would need a joining table but I'm unclear on how to surface it to allow users to select their answers. Would I need to create a controller for this new joining table or am I misunderstanding ActiveRecord in this case?
My Models are:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
class Test < ActiveRecord::Base
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions
end
class Question < ActiveRecord::Base
belongs_to :test
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers
end
class Answer < ActiveRecord::Base
belongs_to :question
end
Any insight/advice on how to best accomplish my goal would be extremely appreciated.
There are different solutions you can try. One way is to set up associations between test and answers through questions.
user.rb
class User < ActiveRecord::Base
has_one :test
end
test.rb
class Test < ActiveRecord::Base
belongs_to :user
has_many :answers, dependent: :destroy
has_many :questions, through: :answers
accepts_nested_attributes_for :answers, allow_destroy: true
end
question.rb
class Question < ActiveRecord::Base
has_many :answers, dependent: :destroy
end
answer.rb
class Answer < ActiveRecord::Base
belongs_to :test
belongs_to :question
end
As for allowing users to select answers, you may need to set up separate associations for an answer to have many selected_answers and many possible_answers through selected_answers. Maybe start with getting tests and answers set up and then move on to selecting answers.
So I have 4 models..
A User model, a Question model an Answer model and a User_Question model.
Now I've created default seed questions that apply to all users i.e. #questions = Question.all
And these same questions every user can see, now how can I allow each user to write their own answer to these questions when they aren't directly associated with the question? I was given a solution to create a has_many through association, I just want to make sure I've set it up correctly please see code below, thanks:
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :user_questions
has_many :questions, through: :user_questions
end
answer.rb
class Answer < ActiveRecord::Base
attr_accessible :answer
has_many :user_questions
has_many :questions, through: :user_questions
end
question.rb
class Question < ActiveRecord::Base
attr_accessible :title, :body
belongs_to :user
has_one :answer
end
user_question.rb
class UserQuestion < ActiveRecord::Base
belongs_to :user
belongs_to :question
belongs_to :answer
end
If I understand this correctly you say your questions exist independently of users. Yet questions belongs to users.
My understand of this should be as follows:
User.rb
has_many :questions
has_many :answers
question.rb
belongs_to :user
has_many :answers
answer.rb
belongs_to :question
belongs_to :user
Notice the plurals as well for belongs_to and has_many.
The link for the guide is here but I don't think you need user_questions.
http://guides.rubyonrails.org/association_basics.html#the-has-many-association