In my Rails project I have a User that can have many Projects which in turn can have many Invoices. Each Invoice can have many nested Items.
class Invoice < ActiveRecord::Base
attr_accessible :number, :date, :recipient, :project_id, :items_attributes
belongs_to :project
belongs_to :user
has_many :items, :dependent => :destroy
accepts_nested_attributes_for :items, :reject_if => :all_blank, :allow_destroy => true
validates :project_id, :presence => true
def build_item(user)
items.build(:price => default_item_price(user), :tax_rate => user.preference.tax_rate)
end
def set_number(user)
self.number ||= (user.invoices.maximum(:number) || 0).succ
end
end
class InvoicesController < ApplicationController
def new
#invoice = current_user.invoices.build(:project_id => params[:project_id])
#invoice.build_item(current_user)
#invoice.set_number(current_user)
#title = "New invoice"
end
def create
#invoice = current_user.invoices.build(params[:invoice])
if #invoice.save
flash[:success] = "Invoice created"
redirect_to invoices_path
else
render :new
end
end
end
Now, I am trying to test the creation of invoices with RSpec and FactoryGirl and all tests pass except for the ones related to the POST create action, such as:
it "saves the new invoice in the database" do
expect {
post :create, invoice: attributes_for(:invoice, project_id: #project, items_attributes: [ attributes_for(:item) ])
}.to change(Invoice, :count).by(1)
end
It keeps giving me this error:
Failure/Error: expect {
count should have been changed by 1, but was changed by 0
Can anybody tell me why this happens?
This is my factories.rb which I use to fabricate objects:
FactoryGirl.define do
factory :invoice do
number { Random.new.rand(0..1000000) }
recipient { Faker::Name.name }
date { Time.now.to_date }
association :user
association :project
end
factory :item do
date { Time.now.to_date }
description { Faker::Lorem.sentences(1) }
price 50
quantity 2
tax_rate 10
end
end
Can anybody help?
Thanks...
Related
I am following Ryan Bates railscasts video of friendly url. I am trying to implement that on my Category model by overriding the to_parammethod.
Seems like it's not working, or I am missing something.
Below is my url before overriding:
localhost:3000/search?category_id=1
After overriding the to_param the url remained same.
Following is my code:
Category model
class Category < ActiveRecord::Base
enum status: { inactive: 0, active: 1}
acts_as_nested_set
has_many :equipments, dependent: :destroy
has_many :subs_equipments, :foreign_key => "sub_category_id", :class_name => "Equipment"
has_many :wanted_equipments, dependent: :destroy
has_many :services, dependent: :destroy
validates :name, presence: true
validates_uniqueness_of :name,message: "Category with this name already exists", scope: :parent_id
scope :active, -> { where(status: 1) }
def sub_categories
Category.where(:parent_id=>self.id)
end
def to_param
"#{id} #{name}".parameterize
end
end
Controller
def search_equipments
begin
if (params.keys & ['category_id', 'sub_category', 'manufacturer', 'country', 'state', 'keyword']).present?
if params[:category_id].present?
#category = Category.active.find params[:category_id]
else
#category = Category.active.find params[:sub_category] if params[:sub_category].present?
end
#root_categories = Category.active.roots
#sub_categories = #category.children.active if params[:category_id].present?
#sub_categories ||= {}
Equipment.active.filter(params.slice(:manufacturer, :country, :state, :category_id, :sub_category, :keyword)).order("#{sort_column} #{sort_direction}, created_at desc").page(params[:page]).per(per_page_items)
else
redirect_to root_path
end
rescue Exception => e
redirect_to root_path, :notice => "Something went wrong!"
end
end
route.rb
get "/search" => 'welcome#search_equipments', as: :search_equipments
index.html.erb
The line which is generating the url
<%= search_equipments_path(:category_id => category.id ) %>
You are generating URLs in such a way as to ignore your to_param method. You're explicitly passing a value of only the ID to be used as the :category_id segment of your URLs. If you want to use your to_param-generated ID, then you need to just pass the model to the path helper:
<%= search_equipments_path(category) %>
This POST create test is not working. It should redirect to reports_path, but it does not work.
describe "POST #create" do
let(:report) { assigns(:report) }
let(:test_option) { create(:option) }
let(:test_student) { create(:student) }
context "when valid" do
before(:each) do
post :create, params: {
report: attributes_for(:report, student: test_student,
report_options_attributes: [build(:report_option).attributes]
)
}
end
it "should redirect to reports_path" do
expect(response).to redirect_to reports_path
end
My params are set like this:
def report_params
params.require(:report).permit(:student,
report_options_attributes: [:id, :option, :note, :_destroy]
)
end
Controller:
def create
#report = Report.new(report_params)
if #report.save
redirect_to reports_path
else
render :new
end
end
Report model:
class Report < ApplicationRecord
belongs_to :student, dependent: :delete
has_many :report_options, dependent: :destroy
accepts_nested_attributes_for :report_options, allow_destroy: true
end
ReportOption model:
class ReportOption < ApplicationRecord
belongs_to :option
belongs_to :report, optional: true
end
I am passing the correct params in the test, but I really do not know what is going wrong..
I've had a similar problem. I fixed it passing just the ids in the params like that:
def report_params
params.require(:report).permit(:student_id,
report_options_attributes: [:id, :option_id, :note, :_destroy]
)
end
Don't know if it will work for you. You also need to change the student: test_student to student_id: test_student.id inside your before(:each).
Hi i have this problem running the spec file.
this is the reparator model:
class Reparator < User
include Mongoid::Document
include Mongoid::Timestamps
field :private_reparator, :type => Boolean, :default => true
field :brand_name, :type => String
field :year_of_experience, :type => Integer, :default => 1
has_many :reparations
has_many :skills
validates_presence_of :skills, :year_of_experience
validates :year_of_experience, :numericality => {:greater_than_or_equal_to => 0}
end
This is the skill model:
class Skill
include Mongoid::Document
field :name, :type => String
belongs_to :reparator
validates_presence_of :name
validates_uniqueness_of :name
end
This is the controller:
class ReparatorsController < ApplicationController
respond_to :json
def index
#reparators = Reparator.all
respond_with #reparators
end
def show
#reparator = Reparator.find(params[:id])
respond_with #reparator
end
def create
#reparator = Reparator.new(params[:reparator])
#reparator.skills = params[:skills]
if #reparator.save
respond_with #reparator
else
respond_with #reparator.errors
end
end
def update
#reparator = Reparator.find(params[:id])
if #reparator.update_attributes(params[:reparator])
respond_with #reparator
else
respond_with #reparator.errors
end
end
def destroy
#reparator = Reparator.find(params[:id])
#reparator.destroy
respond_with "Correctly destroyed"
end
end
And this is the spec file for this controller (i'll just put the test that does't pass):
it "Should create an reparator" do
valid_skills = [FactoryGirl.create(:skill).id, FactoryGirl.create(:skill).id]
valid_attributes = {:name => "Vianello",
:email => "maremma#gmail.com",
:address => "viale ciccio",
:private_reparator => true
}
post :create, :reparator => valid_attributes, :skills => valid_skills
assigns(:reparator).should be_a Reparator
assigns(:reparator).should be_persisted
end
And this is the skill Factory girl:
FactoryGirl.define do
factory :skill do
sequence(:name) {|n| "skill#{n}"}
end
end
I think there is a typo in your spec. post :create, :reparator => valid_attributes, :skills => skills_ttributes should be post :create, :reparator => valid_attributes, :skills => skills_attributes instead.
The bad line is this one
#reparator.skills = params[:skills]
params[:skills] is an array of strings (the ids that have been passed) but the skills= method expects to be given actual instances of Skill and so blows up.
As well as skills=, mongoid also gives you a skill_ids= method which allows you to change which objects are associated by just assigning an array of ids. Alternatively, load the skills object your self and then to #reparator.skills = skills
I'm working on a project where there are tasks that make up a scavenger hunt. When a user creates a new hunt, I'd like the hunts/show.html.erb file to show the hunt as well as the tasks associated with that hunt. But the models are giving me trouble. I've got the hunt model setup to that it accepts nested attributes for the tasks model. So when the user creates a new hunt, she also creates three tasks automatically. I can get the new hunt to save, but I can't get those new tasks to save. Here are my models.
What's missing? Do I need an "attr accessible" statement in the HunTasks.rb file?
class Hunt < ActiveRecord::Base
has_many :hunt_tasks
has_many :tasks, :through => :hunt_tasks
accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
attr_accessible :name
validates :name, :presence => true,
:length => { :maximum => 50 } ,
:uniqueness => { :case_sensitive => false }
end
class Task < ActiveRecord::Base
has_many :hunt_tasks
has_many :hunts, :through => :hunt_tasks
attr_accessible :name
validates :name, :presence => true,
:length => { :maximum => 50 } ,
:uniqueness => { :case_sensitive => false }
end
class HuntTask < ActiveRecord::Base
belongs_to :hunt # the id for the association is in this table
belongs_to :task
end
Here's what my Hunt controller looks like:
class HuntsController < ApplicationController
def index
#title = "All Hunts"
#hunts = Hunt.paginate(:page => params[:page])
end
def show
#hunt = Hunt.find(params[:id])
#title = #hunt.name
#tasks = #hunt.tasks.paginate(:page => params[:page])
end
def new
if current_user?(nil) then
redirect_to signin_path
else
#hunt = Hunt.new
#title = "New Hunt"
3.times do
hunt = #hunt.tasks.build
end
end
end
def create
#hunt = Hunt.new(params[:hunt])
if #hunt.save
flash[:success] = "Hunt created!"
redirect_to hunts_path
else
#title = "New Hunt"
render 'new'
end
end
....
end
The major difference between your example and the railscast is that you are doing many-to-many instead of one to many (I think his was Survey had many Questions). Based on what you described, I wonder if the HuntTask model is necessary. Are the tasks for one hunt ever going to be resused in another hunt? Assuming they are, then looks like your answer is here:
Rails nested form with has_many :through, how to edit attributes of join model?
You'll have to modify your new action in the controller to do this:
hunt = #hunt.hunt_tasks.build.build_task
Then, you'll need to change your Hunt model to include:
accepts_nested_attributes_for :hunt_tasks
And modify your HuntTask model to include:
accepts_nested_attribues_for :hunt
class Job < ActiveRecord::Base
has_many :employments, :dependent => :destroy
has_many :users, :through => :employments
class User < ActiveRecord::Base
has_many :employments
has_many :jobs, :through => :employments
class Employment < ActiveRecord::Base
belongs_to :job
belongs_to :user # Employment has an extra attribute of confirmed ( values are 1 or 0)
In my view i am trying to update the confirmed fied from 0 to 1 on user click.
<%= link_to "Confirm Job", :action => :confirmjob, :id => job.id %>
In my job Controller I have
def confirmjob
#job = Job.find(params[:id])
#job.employments.update_attributes(:confirmed, 1)
flash[:notice] = "Job Confirmed"
redirect_to :dashboard
end
I am sure this is all wrong but I seem to be guessing when it comes to has_many: through.
How would I do update the confirmed field in a joined table?
I think that a job is assigned to a user by the employment. Thus, updating all employments is not a good idea, as Joel suggests. I would recommend this:
class Employment
def self.confirm!(job)
employment = Employment.find(:first, :conditions => { :job_id => job.id } )
employment.update_attribute(:confirmed, true)
end
end
from your controller
#job = Job.find(params[:id])
Employment.confirm!(#job)
This implies that one job can only be taken by one user.
Here is a stab at it (not tested):
def confirmjob
#job = Job.find(params[:id])
#jobs.employments.each do |e|
e.update_attributes({:confirmed => 1})
end
flash[:notice] = "Job Confirmed"
redirect_to :dashboard
end