Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 将自由格式sql转换为活动记录_Ruby On Rails_Postgresql_Haml_Model Associations - Fatal编程技术网

Ruby on rails 将自由格式sql转换为活动记录

Ruby on rails 将自由格式sql转换为活动记录,ruby-on-rails,postgresql,haml,model-associations,Ruby On Rails,Postgresql,Haml,Model Associations,我希望能够在RubyonRails项目中使用这个sql查询 质疑 我有我的模型设置如下 场地 has_and_belongs_to_many :menu_items has_and_belongs_to_many :menu_attributes_menu_items_venues 菜单属性 belongs_to :menu_item belongs_to :venue 菜单项 has_and_belongs_to_many :venue has_and_belongs_to_many :o

我希望能够在RubyonRails项目中使用这个sql查询

质疑

我有我的模型设置如下

场地

has_and_belongs_to_many :menu_items
has_and_belongs_to_many :menu_attributes_menu_items_venues
菜单属性

belongs_to :menu_item
belongs_to :venue
菜单项

has_and_belongs_to_many :venue
has_and_belongs_to_many :orders
has_and_belongs_to_many :menu_attributes_menu_items_venues
然后我希望能够在我的HAML模板中做类似的事情

- for item in @venue.menu_items
        .card
          %img{:src => "http://placehold.it/400x200?text=Coffee"}
            .card-divider
              = item.name
            .card-section
              %h4= item.menu_attributes.name.price
试试这个:

MenuAttribute.joins([{ menu_item: :menu_attributes_menu_items_venues }, :venue]).
    where('menu_items.id = menu_attributes_menu_items_venues.menu_item_id').
    where('menu_attributes.id = menu_attributes_menu_items_venues.menu_attribute_id').
    where('venues.id = menu_attributes_menu_items_venues.venue_id').
    select("menu_items.name", "menu_attributes.name", "menu_attributes_menu_items_venues.menu_attribute_value")
如果关联设置正确并且表具有正确的外键,那么实际上您甚至不需要那些
where
caluse

所以,你应该可以这样做:

MenuAttribute.joins([{ menu_item: :menu_attributes_menu_items_venues }, :venue]).
    select("menu_items.name", "menu_attributes.name", "menu_attributes_menu_items_venues.menu_attribute_value")
让我知道它是否有效。收到您的反馈后,我可以更新我的答案。

试试以下方法:

MenuAttribute.joins([{ menu_item: :menu_attributes_menu_items_venues }, :venue]).
    where('menu_items.id = menu_attributes_menu_items_venues.menu_item_id').
    where('menu_attributes.id = menu_attributes_menu_items_venues.menu_attribute_id').
    where('venues.id = menu_attributes_menu_items_venues.venue_id').
    select("menu_items.name", "menu_attributes.name", "menu_attributes_menu_items_venues.menu_attribute_value")
如果关联设置正确并且表具有正确的外键,那么实际上您甚至不需要那些
where
caluse

所以,你应该可以这样做:

MenuAttribute.joins([{ menu_item: :menu_attributes_menu_items_venues }, :venue]).
    select("menu_items.name", "menu_attributes.name", "menu_attributes_menu_items_venues.menu_attribute_value")

让我知道它是否有效。我可以在收到您的反馈后更新我的答案。

没有办法通过活动记录?纯活动记录来实现这一点?我想没有。对于@venue.menu\u items.card%img{:src=>“”}中的项目,我已经做了这么多。card divider=item.name.card section%h4-对于item.menu\u属性中的attr,没有办法通过活动记录实现这一点?纯活动记录?我想没有。我得到了这一点-对于@vention.menu\u items.card%img{:src=>“”}中的项。card divider=item.name.card节%h4-对于item.menu\u属性中的attr