English 中文(简体)
:通过一个老板——和_归属者_to_many协会
原标题:has_many :through a has_and_belongs_to_many association

我试图在关于铁路项目的鲁比项目中做以下工作:

class FoodItem < ActiveRecord::Base
  has_and_belongs_to_many :food_categories
  has_many :places, :through => :food_categories
end

class FoodCategory < ActiveRecord::Base
  has_and_belongs_to_many :food_items
  belongs_to :place
end

class Place < ActiveRecord::Base  
  has_many :food_categories
  has_many :food_items, :through => :food_category
end

但称之为“试样方法”some_food_item.places。 我有以下错误:

ActiveRecord::StatementInvalid: PGError: ERROR:  column 
food_categories.food_item_id does not exist
LINE 1: ...laces".id = "food_categories".place_id    WHERE (("food_cate...

: SELECT "places".* FROM "places"  INNER JOIN "food_categories" ON "places".id = "food_categories".place_id    WHERE (("food_categories".food_item_id = 1))

什么是完美的,因为关于粮食电离层和食品箱的HABTMs I有名为的食品分类——食品——项目的制图表。

我必须做些什么才能通过制图表正确检查位置,而不是在<食品-类别表中寻找<食品>项目_id?

最佳回答

我的第一版答案是不正确的,但这是完美的。 我第一次做了两件打字(实际上不会制造检测器的危险),但这次我进行了核实。 还需要一种假想,但这是容易的。 第一,安装花板:

script/plugin install git://github.com/ianwhite/nested_has_many_through.git

安装了伊恩·怀特工场,不间断地工作。 如今,这些模型从测试表一中直接复制,以完成这项工作:

class FoodItem < ActiveRecord::Base
  has_many :food_category_items
  has_many :food_categories, :through => :food_category_items
  has_many :places, :through => :food_categories
end

class FoodCategory < ActiveRecord::Base
  has_many :food_category_items
  has_many :food_items, :through => :food_category_items
  belongs_to :place
end

class FoodCategoryItem < ActiveRecord::Base
  belongs_to :food_item
  belongs_to :food_category
end

class Place < ActiveRecord::Base
  has_many :food_categories
  has_many :food_category_items, :through => :food_categories
  has_many :food_items, :through => :food_category_items
end

如今,“农场”协会也同样发挥作用。 <代码>_instance.food_items and food_item.places both work flawlessly, as well as the briefr Associations involved. 仅供参考,我在此图示着所有外国钥匙的去向:

create_table "food_categories", :force => true do |t|
  t.string   "name"
  t.integer  "place_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "food_category_items", :force => true do |t|
  t.string   "name"
  t.integer  "food_item_id"
  t.integer  "food_category_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "food_items", :force => true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "places", :force => true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end

希望这一帮助!

<><>UPDATE: 这个问题最近几次出现。 我写了一篇文章,,其中阐明你的才能:通过的关系,详细解释。 甚至在Gite Hub有下载和玩.的附带例子。

问题回答

几个月前,我写了,这是关于的一篇文章。 简言之,has_many 铁路公司不允许通过<条码>、_和_造船_to_many的协会。 然而,你可以部分地模仿关系,这样做的就是:

class FoodItem < ActiveRecord::Base
  has_and_belongs_to_many :food_categories
  named_scope :in_place, lambda{ |place|
    {
      :joins      => :food_categories,
      :conditions => {:food_categories => {:id => place.food_category_ids}},
      :select     => "DISTINCT `food_items`.*" # kill duplicates
    }
  }
end

class FoodCategory < ActiveRecord::Base
  has_and_belongs_to_many :food_items
  belongs_to :place
end

class Place
  has_many :food_categories
  def food_items
    FoodItem.in_place(self)
  end
end

将向您提供<条码>部分——食品——项目<>。 您寻求的方法。

使用铁路3.2.13和Rasmus的Im, 您的原始装置现在似乎在HABTM上工作。

我建议用户首先尝试工作。

这样做是正确的,因为你可以在一席上“走许多路”。 从本质上讲,你试图将关系扩大的程度比你真的要好。 HABTM (has_and_belongs_to_many)不是解决大多数问题的非常有力的办法。

就你而言,我建议添加一个称为“粮食作物”的模式,并重新命名您的加入表格。 你们还需要补充主要领域。 之后,建立了你这样的示范协会:

class FoodItem < ActiveRecord::Base
  has_many :food_categories, :through => :food_category_items
  has_many :places, :through => :food_categories
end

class FoodCategory < ActiveRecord::Base
  has_many :food_items, :through => :food_category_items
  belongs_to :place
end

class FoodCategoryItems < ActiveRecord::Base
  belongs_to :food_item
  belongs_to :food_category
end

class Place < ActiveRecord::Base  
  has_many :food_categories
  has_many :food_items, :through => :food_categories
end

注:我还在“Place->”中确定了一个字体;它有:食品-项目。 这应当照顾到你们的需要,并给你额外红利,使其能够在今后为你的粮食作物“就业”模式增加功能。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签