开发者

Action Controller: Exception - ID not found

开发者 https://www.devze.com 2022-12-28 03:40 出处:网络
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 relatio

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 se开发者_运维问答e 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消