Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Have a "has_many" but still can not cascade from one object to another.

category sub category rails m

  • Please log in to reply
2 replies to this topic

#1 jschrock1

jschrock1

    Dispatcher

  • Members
  • 45 posts
  • LocationIndianapolis

Posted 30 October 2013 - 02:34 PM

I had this working with the tutorial "users" and "microposts"..  now trying with my new app.  Im trying to run the following code snippet in my view, cascading from one object to another.  Error comes up indicating job_sub_category is not a method.  undefined method `job_sub_category' for #<JobCategory:0x57a3ab8>

 

 <% @job_categories.each do |job_category| %>
      <tr>
        <td><%= job_category.name %></td>
        <td><%= job_category.job_sub_category.name %></td>

.....

 

I ran a job_categories.method.sort routine and sure enough, there was no link to the sub objects... There was no job... method of any kind...  Here is how I defined the models for each object, noting that I do have the has_many in the JobCategory class.

 

class JobCategory < ActiveRecord::Base
   has_many :jobsubcategories, dependent: :destroy
end
 

class JobSubCategory < ActiveRecord::Base
  belongs_to :jobcategory    
end
 

Any ideas on what im missing?  Curious although I thought I was thorough in my spelling and pluralization..   Although it may make sense, its very frustrating how my Class file name is job_category, then reals eliminates the underscores to create a class JobCategory and JobSubCategory, then I have to pluralize the relationship object name in the "has_many", and then when using the objects in my view I have to go back and use the names with underscores in them.  Oh well, perhaps I still have some name issue.. but its definitely not in the list of methods when running job_category.method  (also for fun tried job_sub_category.method and as I expected there was no relationship method back to job_category)....  I'd appreciate another set of eyes..  thanks in advance,  Jay.

 



#2 stevieing

stevieing

    Dispatcher

  • Members
  • 40 posts

Posted 30 October 2013 - 03:39 PM

the has many relationship should be job_sub_categories. The AR documentation should point you in the right direction for naming conventions.

 

The method should be job_sub_categories as well. It is a has many relationship. I would guess that that will appear in your list of methods.

 

I also think your belong_to should be job_category but I am not 100% sure.

 

Regards

 

Steve.


  • james likes this

#3 jschrock1

jschrock1

    Dispatcher

  • Members
  • 45 posts
  • LocationIndianapolis

Posted 30 October 2013 - 06:13 PM

That did it.. thanks..  I had to start over though, at first something was still messed up prob some naming issues, looked closer at the documentation and it now works and shows the additional methods my models are now:

 

class JobCategory < ActiveRecord::Base
   has_many :job_sub_categories
end

 

class JobSubCategory < ActiveRecord::Base
  belongs_to :job_category
end

 

Changed my viewer a bit, want to show category then the subcategories,  its now as follows:  and all works fine, thanks again.

 

        <td><%= job_sub_category.job_category.name %></td>
        <td><%= job_sub_category.name %></td>
 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users