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.
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
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 %>
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,
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.
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.
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.
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
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.
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.
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
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 ?
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.