Rails 4: Saving User selections with a Nested Model Form - ruby-on-rails

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.

Related

Access value of foreign-key

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

What is the alternative for accepts_nested_attributes_for in the belongs_to model activeadmin resource?

I have a memberships resource and it belongs to user and club. I want to access the parent attributes i.e for club and user and I read that accepts_nested_attributes_for is used for parent side of a relationship. What should I write in my membership model?
I have searched about it both in stackoverflow and activeadmin docs but I did not get a thorough explanation about solving my problem...
My membership model is:
membership.rb
class Membership < ApplicationRecord
require 'csv'
belongs_to :club
belongs_to :user
end
Also what should i write in my membership resource which I have already registered with AA...
You can mention the following :-
1) has_many: memberships #in user model
2) has_many: memberships #in club model
This will help you access parent attributes from child model :-
membership.user, membership.club
Also, you can mention accepts_nested_attributes_for: memberships in user model.
When you write this, you can then build a common form for user and membership and modify both of them simultaneously. To achieve this, you will have to allow membership attributes in users_controller.rb.
The following should work(Similar question):
class Club < ApplicationRecord
has_many :memberships, :dependent => :destroy
has_many :users, :through => :memberships
accepts_nested_attributes_for :membership
end
class User < ApplicationRecord
has_many :memberships, :dependent => :destroy
has_many :clubs, :through => :memberships
accepts_nested_attributes_for :membership
end
class Membership < ApplicationRecord
require 'csv'
belongs_to :club
belongs_to :user
accepts_nested_attributes_for :club
end

How to find all projects where client & user involved in Ruby on Rails

At first, I want to show my associated models as like
#=> client.rb
class Client < ApplicationRecord
has_many :client_assignments, dependent: :destroy
has_many :projects, through: :client_assignments
end
#=> client_assignment.rb
class ClientAssignment < ApplicationRecord
belongs_to :project
belongs_to :client
end
#=> project.rb
class Project < ApplicationRecord
has_many :client_assignments, dependent: :destroy
has_many :clients, through: :client_assignments
validates :client_assignments, presence: true
has_many :user_assignments, dependent: :destroy
has_many :users, through: :user_assignments
validates :user_assignments, presence: true
end
#=> user.rb
class User < ApplicationRecord
has_many :user_assignments, dependent: :destroy
has_many :projects, through: :user_assignments
end
#=> user_assignment.rb
class UserAssignment < ApplicationRecord
belongs_to :project
belongs_to :user
end
The concept is "A project involved with a client & current_user".
I'm struggling for that how to find current_user involved projects where client is specific, For example: I have a client_id & which is 2 so how I find all projects for this client where matching current_user.id.
I don't know how I describe this.
Please let me know if you have any confusion.
I haven't tested this, but I feel like something like this should work:
current_user.projects.joins(:clients).where('clients.id = ?', client_id)
you want all the projects associated w/ the current user and then find all the clients associated w/ those projects and filter by the specific client_id.
This should do the same thing and may be faster because it avoids a join:
current_user.projects.joins(:client_assignments).where(clients_id:client_id)

Rails: Create Model with multiple belongs_to

I searched for quite a long time and couldnt find that problem.
user.erb
has_many :workouts
has_many :result_units
workout.erb
belongs_to :user
has_many :sets
set.erb
belongs_to :workout
has_one :result_unit
result_unit.erb
belongs_to :user
belongs_to :set
1 possible Solution is that ResultUnit dont belong to User. But the question is then how much performance it will cost to query User.workouts.all.sets.all.resultunits.all
How could i create a new ResultUnit for User and Set?
This is a case for using a has_many :through association.
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
Running User.workouts.all.sets.all.resultunits.all will result in numerous queries being executed. A has_many :through however, will execute only a single query and allow the database to optimize the joins between tables.
class User < ActiveRecord::Base
has_many :workouts
has_many :sets, through: :workouts
has_many :result_units, through: :sets
end
Ok, I didn't understand your problem 100%.. but I'm gonna have a stab at it and feel free to downvote if it's not right.
class User
has_many :workouts
has_many :result_units
has_many :sets, through: :workouts
# User.first.workouts
# User.first.result_units
# User.first.sets
end
class Workout
belongs_to :user
has_many :sets
# Workout.first.user
# Workout.first.sets
end
class ResultUnit
belongs_to :user
belongs_to :set
# ResultUnit.first.user
# ResultUnit.first.set
end
class Sets
belongs_to :workout
has_one :result_unit
# Set.first.workout
# Set.first.result_unit
end

Is this the proper way to setup a has_many through association?

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

Resources