I really hope this is not a bad question (as in he’s abusing the
list). However I could use a little direction (even though I’m
reading books, tutorials)
Right now I’m getting a
undefined method `category’ for 4:Fixnum
The “link_to position.category_id.category” is the real problem.
So,
In the Position model I have a belongs_to : category
In the Category model I have a has_one : position
I was under the grand illusion that from this point forward I
could reference fields without additonal methods.
hence, position.category_id.category. If I take out category and just
leave position.category_id that displays fine.
Any hints, tips or lambasting would be greatly appreciated.
Oh did I mention I’m a beginner. (Counting 7 days today)
Not weird at all. View source in the browser and take a look at the raw
html. If you don’t understand what you see, spend more time with Ruby,
less with Rails.
position.category_id (comes from the positions table)
category (comes from the categories table)
so from the examples I’ve seen position.category_id.category should be
the correct syntax.
Which example gave you that idea?
Again, the whole point of Rails associations is to have #category_id
as an implicit intermidiary. Read: almost never called directly. It is
used internally for getting position.category, which is what you really want. Tha’st the point of the whole thing: get you what you
want, with minimum typing.
position.category_id (comes from the positions table)
category (comes from the categories table)
Yes, but you’re not dealing with fields in Ruby / Rails. Rails wraps
all of this into objects. You’re dealing with objects in your view.
so from the examples I’ve seen position.category_id.category should be
the correct syntax.
position.category.category would be my guess. Remember
position.category is an object; your “belongs_to” and “has_many” tags
indicate to the elves living in Rails that they should churn out a
couple of classes wrapping some tables. Basically “position.category”
gives you a CategoryModel model instance that contains the data for
the appropriate category.
As a side note does it matter if I’ve not defined the foreign keys in
the database ?
Nope. Rails creates objects, doesn’t care about the database beyond
the “_id” column existing.
I highly recommend reading the second half of Agile Web D.
with Rails before straying too far from the core Depot example.
Almost all of your questions willl be answered, the new release (as of
yesterday, I think) even has a wonderful chapter on Migrations.
Anyway, something I’ll need to have sink in better is why
postion.category.category worked!
You wrote the explanation , very much appreciated.
I recommend as soon as possible reading the two ActiveRecord chapters,
and everything will be made clear. It has in depth coverage of
“belongs_to” and “has_many”.
Thanks for your help. Yes, I downloaded the AWSWR update last night.
I am reading through the framework section and have done the depot
tut. Admitedly I am trying to break away from depot (though still
looking back).
Anyway, something I’ll need to have sink in better is why
postion.category.category worked!
You wrote the explanation , very much appreciated.
Reading part 2 now , read part 1 the other day. Probably a good idea
to read over (and over ?) and hold closely for reference.
My one problem with AWDWR and in particular the sections on
ActiveRecord is that there is no explanation about where to put
certain code he writes to try things out. As an example, on
acts_as_list, he creates the tables, that is clear, add’s the methods
and that is clear but then gives a method to exame the contents of the
tables. Problem is where does that method go ?
Lastly there is some code for manipulating the data, and that is fine
provided it can be tested through the cosole.
My one problem with AWDWR and in particular the sections on
ActiveRecord is that there is no explanation about where to put
certain code he writes to try things out.
I typically use IRB or the rails console. Sometimes it’s enough to
just study it until you understand why the output is what appears in
the book.
-Curtis
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.