Rails: validate presence of parent_id in has_many association - ruby-on-rails

I have a projects resource that has many tasks. I want to ensure that every task has a project_id by adding validates_presence_of :project_id to the tasks model.
However, when creating a new project with tasks, the project_id won't be available until the record saves, therefore I can't use validates_presence_of :project_id.
So my question is, how do I validate presence of project_id in the task model? I want to ensure every task has a parent.
...
class Project < ActiveRecord::Base
has_many :tasks, :dependent => :destroy
accepts_nested_attributes_for :tasks, :allow_destroy => true
...
class Task < ActiveRecord::Base
belongs_to :project
validates_presence_of :project_id

Your code works:
If you validates_presence_of :project, then as long as the project is there, it will validate. But if your project is unsaved, you could still save the task.
If you validates_presence_of :project_id, then the integer must be there, indicating a saved value.
Here's rSpec that proves the point. If you validate :project_id, you can't save a task without saving the Project.
class Task < ActiveRecord::Base
belongs_to :project
end
/specs/model_specs/task_spec.rb
require File.dirname(__FILE__) + '/../spec_helper'
describe Task do
before(:each) do
#project = Project.new
end
it "should require a project_id, not just a project object" do
task = Task.new
task.project = #project
Task.instance_eval("validates_presence_of :project_id")
task.valid?.should == false
end
it "should not be valid without a project" do
task = Task.new
task.project = #project
Task.instance_eval("validates_presence_of :project")
task.valid?.should == false
task.save.should == false
end
end

See here for the definitive answer :
class Project < ActiveRecord::Base
has_many :tasks, :dependent => :destroy, :inverse_of => :project
accepts_nested_attributes_for :tasks, :allow_destroy => true
class Task < ActiveRecord::Base
belongs_to :project
validates_presence_of :project
Not so elegant if you ask me... It should transparently validate.

Maybe I don't understand something, but it looks like you are trying to cheat rails. Why don't you just do like this:
class Task < ActiveRecord::Base
belongs_to :project
validate_presence_of :project
end

Take a look at this:
https://rails.lighthouseapp.com/projects/8994/tickets/2815-nested-models-build-should-directly-assign-the-parent
One thing I have done in the past is add: validates_presence_of :parent_id, :on => :update. Not great but it helps tighten the net a little.

I think you're having the same issue I dealt with. I have two models, Account and User, and when the account is created the first user is created through a #account.users.build. The User model has a validates_presence_of :account validation.
To make the first user pass validation, I added the following code to my Account model:
before_validation_on_create :initialize_users
def initialize_users
users.each { |u| u.account = self }
end

In reality you need both:
validates_presence_of project
validates_presence_of project_id
That way the task will not be saved in either of the following cases assuming that you have only 2 valid projects in the database, i.e. project id 99 is invalid:
task.project_id = 99
task.save
task.project = Project.new
task.save
I hope this is of help to someone.

Your Project class must define
accepts_nested_attributes_for :tasks
See Nested Model Form on Railscasts for more details on how to make the form.
EDIT:
In your form you should have something like this:
_form.html.erb
<% form_for #project do |f| %>
# project fields...
<% f.fields_for :tasks do |builder| %>
<%= render 'task_fields', :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add task", f, :tasks %></p>
<%= f.submit %>
<% end %>
_task_fields.html.erb
<%= f.label :name, "Task name:" %>
<%= f.text_field :name %>
# task fields...
<%= link_to_remove_fields "Delete task", f, :tasks %>
link_to_add_fields and link_to_remove_fields are methods defined in application_helper to add/delete fields dynamically.

Related

Rails 4 - Invite team mates to project

Im trying to add functionality to my Rails 4 app which allows a user (who creates a project) to invite others to join their project team.
I found this tutorial, which I've found helpful: https://coderwall.com/p/rqjjca/creating-a-scoped-invitation-system-for-rails
To this point, I have the following set up:
User
has_one :profile, dependent: :destroy
Profile
belongs_to :user
has_many :teams, foreign_key: "team_mate_id"
has_many :team_projects, through: :teams, source: :project
has_many :invitations, :class_name => "Invite", :foreign_key => 'recipient_id'
has_many :sent_invites, :class_name => "Invite", :foreign_key => 'sender_id'
Project
belongs_to :profile
has_one :team
has_many :team_mates, through: :team
has_many :invites
Invite
belongs_to :project
belongs_to :sender, :class_name => 'Profile'
belongs_to :recipient, :class_name => 'Profile'
Team
belongs_to :project
belongs_to :team_mate, class_name: "Profile"
In my form, I have:
<%= simple_form_for(#invite, :url => invites_path) do |f| %>
<%= f.hidden_field :project_id, :value => #invite.project_id %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.input :expiry, :as => :date_picker, :label => "When do you need a response to this invitation?" %>
<%= f.submit 'Send' %>
<% end %>
Then in my show (rendered on the projects show) I have:
<%= render :partial => 'projects/invite_team_mate' %>
In my invites controller, I have:
class InvitesController < ApplicationController
def new
#invite = Invite.new
end
def create
#invite = Invite.new(invite_params)
#invite.sender_id = current_user.profile.id
if #invite.save
#if the user already exists
if #invite.recipient != nil
#send existing user email invitation to join project team
InviteMailer.existing_user_invite(#invite).deliver
#Add the user to the user group - inivte rsvp pending
#invite.recipient.project.push(#invite.project)
else
#send new user email invitation to join as a user and this project team
#invite.recipient.project.push(#invite.project)
# InviteMailer.new_user_invite(#invite, new_user_registration_path(:invite_token => #invite.token)).deliver
end
else
# oh no, creating an new invitation failed
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_invite
#invite = Invite.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def invite_params
params[:invite].permit(:email)
end
end
I can't figure out what else needs to happen to make this work.
When I save all this and try to invite an email address, I get this error:
undefined method `project' for nil:NilClass
That happens despite the form I use to send the invite being shown on the projects show page.
You need to add project_id and recipient_id to your invite_params, and add the recipient to your form (as a text_field, or hidden field, depending on your use case):
# controller
def invite_params
params[:invite].permit(:email, :project_id, :recipient_id)
end
# form
<%= simple_form_for(#invite, :url => invites_path) do |f| %>
...
<%= f.hidden_field :recipient_id, :value => get_recipient_id %>
...
<% end %>
Error is due to #invite.project_id, because #invite has no data so it's throwing error
<%= f.hidden_field :project_id, :value => #invite.project_id %>
replace this with or with some other desired logic
select_tag "people", options_from_collection_for_select(#projects, "id", "name")
In controlller
def new
#invite = Invite.new
#projects = current_user.team_projects // here you have to add your logic, for which project you want to invite or let me know
end
I'm finding following code very strange
if #invite.recipient != nil
...
#Add the user to the user group - inivte rsvp pending
#invite.recipient.project.push(#invite.project)
else
#send new user email invitation to join coalfacer and this project team
#invite.recipient.project.push(#invite.project)
...
end
How is that you call the same code #invite.recipient. even if #invite.recipient is Nil?!
By the way, ensure you understand why this code is written for in the controller, what it means
def invite_params
params[:invite].permit(:email)
end
For your convenience, refrain from coping code you don't understand. Also, even if you do so, do that in small portions and try after each one, so you can localize the error if any. Working in small increments is essential. You can't do a horde of changes and then just ask "what's wrong about his plenty of code".
Finally, I suggest you write specific questions, using MCVE principle. You have to extract specific portions of your code relevant to the issue, and be specific on the problem. If you put whole bunch of code, including irrelevant one, it's much much harder to help.

No route matches error: Difference between #model.association vs querying?

Technically, I could get this working, but why my current code doesn't work confuses me. I have a many-to-many relationship in my Events <> Users. This is where my view farts out, saying No route matches: missing required key [:id] ...
<% #event_users.each do |event_user| %>
<%= link_to event_user.user.try(:full_name), user_path(event_user.user) %>
<% end %>
This is my code in my controller. One way that works, one way that doesn't work.
#event = Event.find(params[:id])
#event_users = #event.event_users # This does NOT work
# #event_users = EventUser.where(event_id: 14) # This does work
This is my controller relationships
class User < ActiveRecord::Base
has_many :event_users, dependent: :destroy
has_many :events, through: :event_users
end
class EventUser < ActiveRecord::Base
belongs_to :user
belongs_to :event
validates :user, presence: true
validates :event, presence: true
end
class Event < ActiveRecord::Base
has_many :event_users
has_many :users, through: :event_users
end
What exactly is going on here that I have to query and can't use the association to create a link? When I print out the text, I get the relevant data (the ID), so it should work. I've also tried this below and it still doesn't work.
<%= link_to event_user.user.try(:full_name), user_path(event_user.user_id) %>
You don't need to query #event.event_users, thats what the has_many :through association is for, you can simply use the #event object to get its users:
<% #event.users.each do |user| %>
<%= link_to user.full_name, user_path(user) %>
<% end %>
Note, you can also just pass an object to link_to that rails will automatically use a helper to create a route to users#show, like this:
<%= link_to user.full_name, user %>

uninitialized constant on nested resource form

I have three models, users, reports, and receipts. Users have many reports, and reports have many receipts.
Now, I have a form set up to create, or edit reports. And I need to nest another form to create and edit receipts. I followed the rails guide (section - building a multi modeled form) and edited my models, and have added the build line into my form view but Im getting that 'uninitialized constant' error.
Here are my models:
class Report < ActiveRecord::Base
belongs_to :user
has_many :receipts
attr_accessible :cash_advance, :company, :description, :end_date, :mileage, :report_name,
:start_date, :receipts_attributes
validates_presence_of :company, :description, :end_date, :report_name#, :start_date
validates_uniqueness_of :report_name
accepts_nested_attributes_for :receipts, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
class Receipts < ActiveRecord::Base
belongs_to :report
attr_accessible :account_code, :amount, :company_card, :date, :description, :lobbying_expense, :vendor
end
and my form:
<%# #report.receipts.build %>
<%= form_for([current_user,#report]) do |f| %>
...
<%= f.fields_for ([#report, #report.receipts.build ]) do |receipt| %>
...
<% end %>
<% end %>
my routes (which Im not sure if I should have edited, but I got the same error before I added the receipts resources)
resources :users do
resources :reports do
resources :receipts
end
end
I didnt edit the reports controller since the rails guide didnt show any mention of it, its its only:
def new
#report = current_user.reports.new
end
def edit
#report = current_user.reports.find(params[:id])
end
What am I doing wrong?
edit - I changed my form for the receipts so the form_for takes in [#report, #report.receipts.build] but now I get the error:
uninitialized constant Report::Receipt
How do I get this form to work?
UGH! I messed up when I generated the model and gave it a plural name instead of a singular name. This guy, right here, is a fool.

creating new association while creating the related record in active admin

I've these two models
class Case < ActiveRecord::Base
belongs_to :client, :class_name => 'User'
end
class User < ActiveRecord::Base
has_one :requested_case, :class_name => 'Case', :foreign_key => :requested_case_id
end
and I want to create adminstration Interface for Case model using Active Admin, so when I create new case I can create new client for it in the same time, so I wrote the following lines of codes in the app/admin/cases.rb file
ActiveAdmin.register Case do
form do |f|
f.inputs "Basic Details"
f.input :title
f.input :Description
end
f.inputs :name => "Client Details", :for => :client do |c|
c.input :name
c.input :mobile
end
f.buttons
end
end
so when I filed the inputs of client and click submit I got this error
ActiveRecord::AssociationTypeMismatch in Admin::CasesController#create
User(#-625154418) expected, got ActiveSupport::HashWithIndifferentAccess(#82665960)
so any help please what's missing here?
Just add to your app/admin/cases.rb file
controller do
def new
#case = Case.new
#case.build_client
end
end
and don't forget to add accepts_nested_attributes_for to your case model
accepts_nested_attributes_for :client

Application design of controller (Rails 3)

This is hard to explain, but I will do my best:
I am building a system where user's can take courses. Courses are made up of steps, that must be taken in order. In the system there are 6 step types (Download, Presentation, Video, Text, Quiz, and Survey)
The way a user accesses a STEP currently is:
http://example.com/courses/2/course_steps/1
As you can tell course_steps are nested under courses.
Below is the show method in course steps:
def show
render "show_#{#course_step.step.step_type.name.downcase}"
end
As you can tell it basically picks a view to show_[TYPE] (quiz, survey, text..etc)
This works fine for simple steps such as a text, video, or download, but for complicated steps such as a quiz, this model does not work well for the following reasons:
How do I validate a form for a quiz or survey as I would be using a different controller (QuizAttemptsController).
It seems to break the REST principal as a quiz, survey..etc should be treated separately. (I know they are step types, but they can have their own actions and validations)
Step Model
class Step < ActiveRecord::Base
belongs_to :step_type
belongs_to :client
has_one :step_quiz, :dependent => :destroy
has_one :step_survey, :dependent => :destroy
has_one :step_text, :dependent => :destroy
has_one :step_download, :dependent => :destroy
has_one :step_video, :dependent => :destroy
has_one :step_presentation, :dependent => :destroy
has_many :course_steps, :dependent => :destroy
has_many :courses, :through => :course_steps
has_many :patient_course_steps, :dependent => :destroy
attr_accessible :step_type_id, :client_id, :title, :subtitle, :summary
validates :title, :presence=>true
validates :summary, :presence=>true
def getSpecificStepObject()
case self.step_type.name.downcase
when "text"
return StepText.find_by_step_id(self.id)
when "quiz"
return StepQuiz.find_by_step_id(self.id)
when "survey"
return StepSurvey.find_by_step_id(self.id)
when "download"
return StepDownload.find_by_step_id(self.id)
when "video"
return StepVideo.find_by_step_id(self.id)
when "presentation"
return StepPresentation.find_by_step_id(self.id)
end
end
end
Step Quiz Model:
class StepQuiz < ActiveRecord::Base
belongs_to :step, :dependent => :destroy
has_many :step_quiz_questions, :dependent => :destroy
has_many :quiz_attempts, :dependent => :destroy
accepts_nested_attributes_for :step
accepts_nested_attributes_for :step_quiz_questions, :allow_destroy => true
attr_accessible :step_id, :instructions, :step_attributes, :step_quiz_questions_attributes
validates :instructions, :presence=>true
end
CourseStep Model
class CourseStep < ActiveRecord::Base
belongs_to :step
belongs_to :course
validates_uniqueness_of :step_id, :scope => :course_id
def next_step()
Course.find(self.course.id).course_steps.order(:position).where("position >= ?", self.position).limit(1).offset(1).first
end
def previous_step()
Course.find(self.course.id).course_steps.order("position DESC").where("position <= ?", self.position).limit(1).offset(1).first
end
end
How would you suggest fixing this?
What you want to do is implement your Model as a Finite State Machine and continually reload the new or edit action until the desired state is reached, then your controller can display different views depending on state to allow multiple steps to happen.
One way I have solved the problem is by adding a member action of "submit_quiz"to the course_steps controller. I am not sure if I like this, as the code looks kind of ugly. I would appreciate feedback.(Note: I Am using CanCan so #course_step is created automagically in the course_steps_controller)
The things I don't like are:
show_quiz view has a lot of code in it
submit_quiz is in the course_steps_controller
quiz_attempt model has virtual attribute of quiz_questions (for validation purposes)
show_quiz.html.erb
<%= form_for (#quiz_attempt.blank? ? QuizAttempt.new(:started => Time.now.utc, :step_quiz_id => #course_step.step.step_quiz.id) : #quiz_attempt, :url => submit_quiz_course_course_step_path(#course_step.course, #course_step)) do |f| %>
<%= render :partial => 'shared/error_messages', :object => f.object %>
<% #course_step.step.step_quiz.step_quiz_questions.each do |quiz_question| %>
<h3><%= quiz_question.value %></h3>
<% quiz_question.step_quiz_question_choices.each do |quiz_question_choice| %>
<%= radio_button_tag("quiz_attempt[quiz_questions][#{quiz_question.id}]", quiz_question_choice.value, f.object.get_quiz_question_choice(quiz_question.id) == quiz_question_choice.value)%>
<%= quiz_question_choice.value %><br />
<% end %>
<% end %>
<%= f.hidden_field(:step_quiz_id)%>
<%= f.hidden_field(:started)%>
<%= submit_tag("Submit Quiz")%>
<% end %>
course_steps_controller.rb
def show
PatientCourseStep.viewed(current_user.id, params[:course_id], #course_step.step.id )
render "show_#{#course_step.step.step_type.name.downcase}"
end
def submit_quiz
#quiz_attempt = QuizAttempt.new(params[:quiz_attempt])
if !#quiz_attempt.save()
render 'show_quiz'
end
end
quiz_attempt.rb
class QuizAttempt < ActiveRecord::Base
belongs_to :step_quiz
belongs_to :patient
attr_accessor :quiz_questions
attr_accessible :step_quiz_id, :patient_id, :started, :ended, :correct, :incorrect, :quiz_questions
validate :answered_all_questions?
def get_quiz_question_choice(quiz_question_id)
unless self.quiz_questions.blank?
quiz_questions[quiz_question_id.to_s]
end
end
private
def answered_all_questions?
#Making sure they answered all the questions
if self.quiz_questions.blank? or self.quiz_questions.try(:keys).try(:count) != self.step_quiz.step_quiz_questions.count
errors.add_to_base "Not all questions were answered"
end
end
end
def submit_quiz
#quiz_attempt = QuizAttempt.new(params[:quiz_attempt])
if !#quiz_attempt.save()
render 'show_quiz'
end
end

Resources