1

私はRubyonRailsを初めて使用します。既存のデータベースにWebアプリケーションを構築するだけです。私はレールを使用して、レストランとロケーションテーブル用の2つの足場を生成します。その後、これら2つのテーブルの関係を設定します。

 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

これらのテーブルにこの関係を設定した後は、「rake db:migrate」を使用しませんでした。これは、このアクションによって既存のテーブルが変更されるのではないかと心配したためです。

このコマンドラインを実行すると

<%= restaurant.location.address1%> 

エラーが表示されます:

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> "

その後、ファイルに外部キーを設定しようとしました。

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

しかし、それでも機能しません。

テーブルの関係を設定した後、「railsdb:migrate」を使用する代わりに外部キーを設定できる方法はありますか?よろしくお願いします。

4

3 に答える 3

1

Rails の関連付けについては、Rails Guides で詳しく説明されています

ここでは、基本的なセットアップについて説明します。

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

rake db:migrate次に、データベース テーブルの変更を有効にするためにデータベースを移行する必要があります。

restaurant_id を使用すると、次のようにモデルに関連付けを設定できます

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

これで、次のようにレストランの場所にアクセスできます。

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

さまざまなスタイルの関連付けを少し試してみてください。たとえば、新しい Rails アプリをすばやく作成し、何らかの種類の関連付けを試してみてください。結合モデルが必要なものもあります。

于 2012-11-05T20:35:48.113 に答える
1

問題は、場所を間違って使用していることです。

レストランには多くの場所があるため、あなたが言及した方法では使用できません。場所の配列があるため、実際には ActiveRecord 関係であるため、関連付けられた項目の 1 つにアクセスするには、クエリを実行して要素の 1 つを取得する必要があります。最初の要素を取得する方法の例を次に示します。

restaurant.locations.first.address1

レストランの場所が 1 つしかない場合は、モデルを次のように変更する必要があります。

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

あなたがしているのと同じようにプロパティにアクセスします:

restaurant.location.address1

また、データベースには指定した列があると想定しています。そうでない場合は、移行を実行する必要があります。

よろしく!

于 2012-11-05T19:58:07.747 に答える
0

今、私はこの方法を試してみましたが、うまくいきました。どうもありがとうございました。

<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>
于 2012-11-06T21:05:56.153 に答える