Topic: Why won't this work?

Maybe I'm just too tired to figure this out.. I have a Stores table in my database populated with stores with id's ranging from 1 through 100.  I'm trying to grab one of them using this code:

@place = Store.find(:all, :conditions => "id = '10'")
warn @place.id

When I do that, I want to see @place.id also show me 10 but I'm getting some random number like 18881682.  The above code is not in my stores_controller.rb but rather in a controller called events_controller.rb

Full code for the controller action looks like this where @event.venue returns 10 correctly:

  def show
    @event = Event.find(params[:id])
    @place = Store.find(:all, :conditions => ['id = ?', @event.venue])
    warn @event.venue
    warn @place
  end

Thanks..

Re: Why won't this work?

Wait, so is there a has_many/has_one type relationship between Event and Store? If so, your "venue" column in the events table should be "store_id", and the association will automatically pick up (if you have has_many/has_one set). Then you could do things like @event.store.name instead of having to explicitly create a Store object to get to its attributes.

vinnie - rails forum admin

Re: Why won't this work?

I didn't have a has_many or belongs_to relationship set up, but I added it just now.  So my stores model has: 

has_many :events

and my events model has:

belongs_to :stores

My warn statement:
warn @event.store.name

gives an error: undefined method `store' for #<Event:0x2355bd0>

A different warn statement: warn @event.id.store.name
gives a different error: undefined method `store' for 4:Fixnum

(where 4 is the event ID)

Re: Why won't this work?

Shouldn't that be singular rather than plural

belongs_to :store

Re: Why won't this work?

Baltar wrote:

Shouldn't that be singular rather than plural

belongs_to :store

Could be.. not sure. I thought it would be plural.  In any case, if I make it singular (as in your example) I get the same error..

Re: Why won't this work?

Did you change your "venue" column to "store_id" to match the relationship between the events and stores tables?

vinnie - rails forum admin

Re: Why won't this work?

I did..

I think I solved it another way though.  In my view I am now passing an extra parameter (loc) as so:

  <%=link_to evt.send('event_head'), :controller => 'events', :action =>'eventshow', :id => evt.send('id'), :loc => evt.send('store_id') %>

Then, as you mentioned, I am able to pull up the location by using @event.store.store_name

So, I guess it's solved!