Action Controller: Exception - ID not found - ruby-on-rails

I am slowly getting the hang of Rails and thanks to a few people I now have a basic grasp of the database relations and associations etc. You can see my previous questions here: Rails database relationships
I have setup my applications models with all of the necessary has_one and has_many :through etc. but when I go to add a kase and choose from a company from the drop down list - it doesnt seem to be assigning the company ID to the kase.
You can see a video of the the application and error here: http://screenr.com/BHC
You can see a full breakdown of the application and relevant source code at the Git repo here: http://github.com/dannyweb/surveycontrol
If anyone could shed some light on my mistake I would be appreciate it very much!
Thanks,
Danny

You have setup your Kase and Company models as a one-to-one relationship (see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html). This is probably not what you intended. Maybe if you could explain your intended relationship I could tell you where your mistake is?
class Company < ActiveRecord::Base
has_many :kases
has_many :people
end
class Kase < ActiveRecord::Base
belongs_to :company # foreign key: company_id
has_and_belongs_to_many :people # foreign key in join table
end
class Person < ActiveRecord::Base
has_and_belongs_to_many :kases # foreign key in join table
end
Relevant parts shown only. This should be a step in the right direction. You will need a join table for the many-to-many relationship, or alternatively, to model it using "has_many :through". Depends on whether you need to store other properties on the join. See link above for details.

I believe It should be
class Company < ActiveRecord::Base
has_many :people
has_many :kases
end
class Kase < ActiveRecord::Base
belongs_to :company
belongs_to :person
end
class Person < ActiveRecord::Base
belongs_to :company
has_one :kase
end
In your view (app/views/kases/new.html.erb) you have
<li>Company Select<span><%= f.select :company_id, Company.all %></span></li>
Try changing the select part to
<%= f.select :company_id, Company.all.collect {|m| [m.name, m.id]} %>
Suggestion
I also notice that you have four methods in your controller to find Kases by status. You can do this in your model, using named_scope. It's like this:
named_scope :active, :conditions => {:kase_status => 'Archived'}
And then, wherever you need to show only active Kases, you call Kase.active. The same for the other status.

Related

Should a link 'has one' or 'belongs_to' a category?

I'm designing a simple Rails app with 2 models: Link and Category. Each link has exactly one category, and a category can have 0 or multiple links. What association should I apply to Link and Category? I'm very new to Ruby and those association terms are so confused to me.
You said each link has exactly one category
class Link < ApplicationRecord
belongs_to :category
end
category should exist before the link, so we go for belongs_to, not has_one
You said a category can have 0 or multiple links, so 0, 1, 2 or 79.
class Category < ApplicationRecord
has_many :links
end
I would suggest first to read active record associations
In your case
class Link
belongs_to :category
end
class Category
has_many :links
end
Don't get confused by the semantics when it gets to has_one and belongs_to.
The key difference is that belongs_to puts the foreign key on this models table:
class Link < ApplicationRecord
belongs_to :category
end
class Category < ApplicationRecord
has_many :links
end
You should read belongs_to :category as "this models table has a category_id column that references categories, it can only belong to a single category".
If you instead used:
class Link < ApplicationRecord
has_one :category
end
Rails would attempt to resolve the association through the categories.link_id column which won't work at all. has_one should be used when you have a one to one association on the the side without the foreign key:
class Country
has_one :capitol
end
class Capitol
belongs_to :country
end

Polymorphic relationship vs STI vs Class table inheritance with RoR

There are three types of invoice items with following tables
1) SubscriptionItems,
2) Prorations,
3) UsageItems,
Those have the same attributes below
invoice_id
amount
stripe_invoie_id
However
only SubscriptionItem and Proration
period_start_at
period_end_at
and only Proration and UsageItem has
title
and only UsageItem has
uuid
account_id
description
To achieve this model I've been using polymorphic relation.
class InvoiceItem < ActiveRecord::Base
belongs_to :invoice
belongs_to :itemable, polymorphic: true
end
class SubscriptionItem < ActiveRecord::Base
belongs_to :plan
has_one :invoice_item, as: :itemable
end
class UsageItem < ActiveRecord::Base
belongs_to :account
has_one :invoice_item, as: :itemable
end
class Invoice < ActiveRecord::Base
belongs_to :account
has_many :invoice_items
end
class Account < ActiveRecord::Base
has_many :invoices
has_many :usage_items
end
For now it works.
However As far as I understand polymorphic should have has_many relation.
So this resides in the middle of Polymorphic and STI.
Because those three types of invoice items are always be subscriptionitem, proration, or usageitem.
It's hard decision that I could keep using this models (polymorphic with has_one) or should I use STI instead?
Or class table inheritance should be fit?
EDIT
I'd love to hear the reason why I could use some design.
Maybe those types pros and cons.
As far as I know,
If I apply STI
That leads many NULLable columns, but RoR supports STI. So it's easy
to use.
If I apply polymorphic with has_one
It stills the rails way but the original polymorphic definition is
different. It should have has_many relationship instead of
has_one. Also it's impossible to add foreign key.
Ref: Blog post for STI to polymorphic
If I apply Class table inheritance,
It's more efficient for relational database, but it's not rails way.
Ref: Blog post for STI to class table inheritance
I think STI with a hidden_field passing the appropriate value for each attribute that should determine the invoice type, could be the way to go here. It's simple and efficient.
Let's say you added a field called :invoice_type to your invoice model,
Then just loop through the items in an array like (Rough example):
<% #invoices.where(:invoice_type => "proration").find_each do |invoice| %>
<% #invoices.where(:start_date => "#{#invoice.start_date}").find_each do |invoice| %>
<!--Will only show the start_date of invoices where the invoice_type is propration. -->
<% end %>
<% end %>

Model association between Company, Employee, and department

I am working in Ruby on Rails 3. And trying to map out three models which mimic the data of a Company its employees and their respective departments.
In arrived at the following solution:
class Company < ActiveRecord::Base
has_many :departments
has_many :employees, through => :departments
end
class Department < ActiveRecord::Base
belongs_to :company
has_many :employees
has_one :department_description
end
class DepartmentDescription < ActiveRecord::Base
belongs_to :department
end
class Employee < ActiveRecord::Base
belongs_to :department
end
Is this the 'correct' way to associate these models?
I think your last response may explain why you are struggling to find a correct way to associate these models.
It seems that you see your Department merely as a join_table, and that may be due to the fact that you don't fully understand the has_many => :through construction and that it actually allows your Department to be a proper model with many attributes and methods in it, hence also a 'description' attribute.
To create a separate DepartmentDescription model is actually a waste of resource. Chad Fowler has a few good examples for :has_many => through and nested resources in his Rails Recipes... so check that out.

Foreign key in Ruby Rails

Hey I need help with the following set-up, I cant seem to find a solution:
User.rb
class User < ActiveRecord::Base
has_one :education
end
Education.rb
class Education < ActiveRecord::Base
belongs_to :user
end
-The Users table holds 'id'(id of the user) and 'education_id' and other columns which are of no importance now.
-The Educations table holds 'id' and 'name' which is the name of the education.
I'd like to get the Education name by using the *education_id* in the Users table to link to id in Educations.
I want to be able to use that in the view by using some syntax like
<%= user.education %>
I believe its a real simple solution but I cant seem to find it
Cheers
Ref this
As per your Model declaration you should have user_id column in your educations table.
OR you have to change your model declaration to following
class User < ActiveRecord::Base
belongs_to :education
end
class Education < ActiveRecord::Base
has_one :user
end
by seeing the comment i think you need proper explanation
first of all do
class User < ActiveRecord::Base
belongs_to :education
end
class Education < ActiveRecord::Base
has_one :user
end
the table which has the foreign key in this case education_id should have belongs_to( in this case ) user and the table of which the foreign key is created here education_id has has_one

In Ruby on Rails, how can a model "has_many" and "belong_to" using a different field other than primary ID?

I am trying building a simple project from scratch using Rails 3. Usually, the models are like:
class Student < ActiveRecord::Base
has_many :awards
end
class Award < ActiveRecord::Base
belongs_to :student
end
and we use the award.id and student.id to get the corresponding records.
But what if it is
class Company < ActiveRecord::Base
has_many :stock_quotes
end
class StockQuote < ActiveRecord::Base
belong_to :company
end
In this case, we can use the symbol of the company, such as MSFT or GOOG to identify the company, instead of using the company.id. For example, in stock_quotes, we can store the symbol of the company directly instead of using company.id. In this case, is there a way to specify it in the models?
http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to
http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many
Check out :primary_key and :foreign_key options
In addition to Slawosz' answer, this question about non-integer primary keys is also relevant to your question. IMHO, it would be easier to just use integer id's like in the example of Award and Student.
Slawosz has the answer right. To be verbose (and for the next time I search this) it should be like this:
#company
has_many :stock_quotes, :primary_key => :symbol
#stock_quote
belongs_to :company, :foreign_key => :symbol

Resources