Method for associated relationships

I have these tables set up like this:

listings has_many :states
state_id [int]
<other fields…

…>

states belongs_to: listings
name
<other fields…

…>

In my view I have <%= listing.name %>
have also tried listing.state_id.name , that didn’t seem to do the magic
either.

This is the error message:
NoMethodError in Company#list

Showing app/views/company/list.rhtml where line #29 raised:

undefined method `name’ for 7:Fixnum

basically the action code is: find(:all)
What am I doing wrong ?

TIA
Stuart

The state_id field goes always inside the model that belongs_to the
state
model.

At first glance it doesn’t make much sense for states to belong_to
listings,
so I suspect that what you want is to reverse the association, leaving
the
state_id field where it is.

  • foobario

You have your foreign key the wrong way round if listings have many
states. The ‘states’ table should have a ‘listing_id’.

Then you can access a listing’s states like this:

listing.states

That’ll give you a collection of states:

<% listing.states.each do |state| %>
<%= state.name %>

<% end %>

That will print the name of each state related to the listing.

To access the listing via the state, you just do:

state.listing

So you’d access an attribute of listing like this:

<%= state.listing.whatever %>

Hope that helps!

Steve

No sorry that isn’t the goal , apologies for bad explanation , I’ll try
better now.
table listings:
has a field called state_id (this is where a number would go that
corresponds to the id in the states table.
states table is like this (id, name)
1 Alaska
2 Alabama

listings table would have an entry in the state_id field i.e. 2
so the view for the listings table should display Alabama

Stuart

Ok - So a listing belongs_to a state.

<%= listing.state.name %>

should work.

That’s assuming:

listings

id
state_id

states

id
name

class Listing < ActiveRecord::Base
belongs_to :state
end

class State < ActiveRecord::Base
has_many :listings
end

Steve

Dark A. wrote on 14.07.2006 21:01:

name

Showing app/views/company/list.rhtml where line #29 raised:

undefined method `name’ for 7:Fixnum

You should read more documentation…

I think your associations are wrong or you table descriptions misses
something.
I see only state_id in listings table.
So your Listing has a belongs_to :state
and your State has_one :listing
No more associations available.

To access variables from an action in the view, the vars must be
instance variables.
So there should be <%= @listing.state.name %>

Markus

On 7/14/06, Stephen B. [email protected] wrote:

Steve
To me this looks wrong. It didn’t work either so not sure what’s going
on.
It looks though as it would be reverse. listings could have many states,

Stuart

Bear with me a moment, because my database skills are not the greatest.
So
I’m thinking this through out loud:
1 listing can only have one state associated with it.
The listings tables can have many states within it.
The states table wouldn’t have multiple entries for a state.

That make sense ?

If listings can have many states, and (I’m assuming) states can have
many
listings, it sounds like you need a has_and_belongs_to_many association.

  • foobario

Based on what you just said:

The states table wouldn’t have multiple entries for a state.

So your states can’t belong_to something else (doing so would tie up
that
state entry).

1 listing can only have one state associated with it.

A listing only associates to one state. We already know that the state
can’t belong_to the listing, so it looks like the listing belongs_to
State.

The listings tables can have many states within it.

…which is another way of saying that State has_many :listings.

I know from recent personal experience that my first-order guess at how
a DB
should be formed was often exactly backwards. And it gets confusing
thinking about whether an association applies to the whole table or to
an
entry in that table. It does get easier with time.

  • foobario

On 7/14/06, foobario [email protected] wrote:

A listing only associates to one state. We already know that the state
can’t belong_to the listing, so it looks like the listing belongs_to State.

The listings tables can have many states within it.

…which is another way of saying that State has_many :listings.

I went back and checked teh r4rmusic1 site form Ruby on Rails to see how
my
tables would fit. I’m saying this because it can be confusing to say
listings table can have many states. I believe though the relationships
are
not set on the table, but on the objects within the table, so a listing
can
only have one state.

Stuart

I know from recent personal experience that my first-order guess at how
a DB

To me this looks wrong. It didn’t work either so not sure what’s going on.
It looks though as it would be reverse. listings could have many states,
If a listing belongs to a state, this is known as a one-to-many
relationship. i.e. One state can have many listings. This is
represented in Rails using the code examples i gave earlier
(belongs_to/has_many).

If listings can have many states and states can have many listings, then
you need to use: has_and_belongs_to_many.

Steve

On 7/14/06, Stephen B. [email protected] wrote:

you need to use: has_and_belongs_to_many.
This statement is confusing me. one listing (in the listings table)
can
only have one state. Is that how I define the relationship or is it
based
on the entire table ?
Stuart

Steve

This statement is confusing me. one listing (in the listings table)
can only have one state. Is that how I define the relationship or is
it based on the entire table ?
I think you might be getting confused about what the relationship refers
to. Don’t think about the table as whole - just think about the model.
You are representing a Listing. A listing can be linked to one state.
One state can be linked to many listings.

For example, you could have 5 listings in California. In rails we say
that a listing ‘belongs_to’ a state and a state ‘has_many’ listings.

The issue you’re having is with database design concepts. Once you
understand them, it’ll all make sense.

Hope this helps!

Steve

Then the actual database:

listings (table):
id primary id

state

states (table)
id primary id
name

You need to have a state_id in the listings table. That should have the
id of the state that the listing belongs to in it.

Looks like your relationships are set up ok and providing you have a
listing linked to a particular state, you should be able to do:

<%= listing.state.name %>

If that still isn’t working, please post any errors you’re getting. If
you’re not getting any errors, check the database and make sure the
actual data is correct; i.e. a listing as a state_id set.

Steve

On 7/14/06, Stephen B. [email protected] wrote:

actual data is correct; i.e. a listing as a state_id set.

Steve

Since you said I need to have a state_id in the listings table I
changed state to state_id.
then I think the view code would be: <%= listing.state_id.name %>
and get this error:
undefined method `name’ for 7:Fixnum

if I leave it as <%= listing.state.name %>
then the error is:
undefined method `state’ for #Position:0x33b6888

Stuart

Well I’m getting no where fast.
I do understand what your saying, so here is my code,maybe something
else is
wrong.

listing model:
class Listing < ActiveRecord::Base
belongs_to :state
def self.current_openings
find(:all)
end
end

state model:
class State < ActiveRecord::Base
has_many :listing
end

Then in the view:

<% for listing in @listings -%>
<%= listing.category_id %>


<%= listing.state.name %>
<% end %>

Then the actual database:

listings (table):
id primary id

state

states (table)
id primary id
name


Okay , it’s finally working.
In the listing model I had “belongs_to :states”
it needed to be “belongs_to :state”
So when creating these relationships I need to use the class name,
lower case ? / singular form ?

Stuart

stuart fellowes wrote:

Okay , it’s finally working.
In the listing model I had “belongs_to :states”
it needed to be “belongs_to :state”
So when creating these relationships I need to use the class name,
lower case ? / singular form ?

Stuart

Hi,
Wen using a relationship statement that implies plurality, e.g. has_many
or has_and_belongs_to many, you use the plural, and when using one that
implies singularity, e.g. has_one or belongs_to, you use the singular.
Hope that helps
Chris

So when creating these relationships I need to use the class name,
lower case ? / singular form ?
Rails relationships are specified like english. For example, when
talking about the listings/states relationship you would say:

A listing belongs to a state
and
A state has many listings

Therefore in rails you define the relationship as follows:

class Listing < ActiveRecord::Base
belongs_to :state
end

class State < ActiveRecord::Base
has_many :states
end

So just think about how you would say the relationship and it should
work.

Steve

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs