jimmy
jimmy

Reputation: 39

Look for help on Ruby on Rails

I am new with Ruby on Rails. I just build web application on the existing database. I use rails to generate 2 scaffolds for restaurant and location tables. After that I set relationship for these two tables:

 class Restaurant < ActiveRecord::Base
  attr_accessible :created, :cuisine_fk, :dish_keywords, :dish_names, :factual_id, :first_name, :last_name, :name, :status


  has_many :locations 
end

class Location < ActiveRecord::Base
  attr_accessible :address1, :address2, :city, :created, :latitude, :longitude, :phone, :restaurant_fk, :state, :status, :url, :zip

  belongs_to :restaurant
 end

I didn't use "rake db:migrate" after I set up this relationship for these tables, because I was afraid that this action would make changes the existing tables.

When I run this command line

<%= restaurant.location.address1%> 

it shows error:

undefined method `location'

" NoMethodError in Restaurants#index

Showing C:/Sites/Dishclips/app/views/restaurants/index.html.erb where line #52 raised:

undefined method `location' for #<Restaurant:0x5853bb8> "

After that I tried to set foreign key for the file:

class Location < ActiveRecord::Base
  attr_accessible :address1, :address2, :city, :created, :latitude, :longitude, :phone, :restaurant_fk, :state, :status, :url, :zip

  belongs_to :restaurant, :class_name => "Restaurant", :foreign_key => 'restaurant_fk'
 end

but it still doen't work.

Is there any way that we can set foreign keys in stead of using "rails db:migrate" after we set up the relationships for tables ? I appreciate your help a lot.

Upvotes: 1

Views: 187

Answers (3)

jimmy
jimmy

Reputation: 39

Now I try this way, then it works. Thank you very much.

<td> 
      <% restaurant.locations.search(params[:restaurant_fk]).each do |location| %>
         <!--address =  <%= location.address1 %> + " " + <%= location.address2 %>-->

         <%= location.address1 %> 
         <%= location.address2 %> ,
         <%= location.city %> ,
         <%= location.state %> ,
         <%= location.zip %> 

      <% end %> 
</td>

Upvotes: 0

Thomas Klemm
Thomas Klemm

Reputation: 10856

Rails associations are covered very well here in the Rails Guides.

I'll walk you through a basic setup here.

$ rails generate model Restaurant name owner ...
$ rails generate model Location restaurant_id:integer city ...

You then need to migrate your database with rake db:migrate for the database table changes to become effective.

The restaurant_id allows us to set the associations in our models as follows

class Restaurant < ActiveRecord::Base
  has_many :locations, dependent: :destroy
  attr_accessible :name, :owner
end

class Location < ActiveRecord::Base
  belongs_to :restaurant
  attr_accessible :city  # no restaurant_id here
end

Now you can access your restaurants location as follows.

r = Restaurant.create!(name: '...')
l = Location.create!(city: '...')

# Add association
r.locations << l

r.locations will now return an Array with l in it

l.restaurant will return r

Try to play a little with the different styles of associations, for example by creating new Rails apps quickly and just trying some kind of associations, also some that require a join model.

Upvotes: 1

felipeclopes
felipeclopes

Reputation: 4070

The problem is that you are using location wrongly.

Since the restaurant has_many locations you can't use it the way you mentioned. Because you have an array of locations, actually is a ActiveRecord relationship, so in order to access one of the items assciated you'll have to execute the query and get one of the elements. Here is an example of how to get the first element.

restaurant.locations.first.address1

If the restaurant have only one location, than you should change your model to

class Restaurant < ActiveRecord::Base
  attr_accessible :created, :cuisine_fk, :dish_keywords, :dish_names, :factual_id, :first_name, :last_name, :name, :status


  has_one :locations 
end

and access the property as you are doing:

restaurant.location.address1

Also I'm assuming that your database have the columns you specified, otherwise you'll have to run the migrations.

Regards!

Upvotes: 1

Related Questions