Undefined method

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 specific lines from the rhtml file are:

<%= link_to position.category_id.category, :action => "show", :id => position.id %>

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)

Stuart TIA

On 6/14/06, Dark A. [email protected] wrote:

=> position.id %>
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)

Stuart TIA


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Try position.category instead of position.category_id.category

(As an aside, you almost never see anything calling #category_id; it’s
an intermediary for more common patterns like the one above)

Using position.category the page displayed a “#”.

Kind of weird.
Stuart

You want something like:
position.category.title or whatever the field/attribute is called that
you want to display

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.

As a side note does it matter if I’ve not defined the foreign keys in
the database ?

Stuart

Dark A. wrote:

Using position.category the page displayed a “#”.

Kind of weird.

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.

The problem has nothing to do with link_to.

What type is position.category_id? (I’ll give you a hint – it begins
with ‘Fix’ and ends with ‘num’.)

On 6/14/06, Dark A. [email protected] wrote:

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.

James,

Yes, I saw the fixnum in the error message but didn’t think that would
matter (I know , I was crazy to think that)

Stuart

On 6/14/06, Dark A. [email protected] wrote:

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. :wink:

-Curtis

On 6/14/06, Dark A. [email protected] wrote:

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. :slight_smile: It has in depth coverage of
“belongs_to” and “has_many”. :wink:

-Curtis

Curtis,

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.

Stuart

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.

Stuart

On 6/14/06, Dark A. [email protected] wrote:

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. :slight_smile:

-Curtis