Rails:从没有模型的表中检索值(Rails: retrieve values from a table without a model)

编程入门 行业动态 更新时间:2024-10-24 16:27:34
Rails:从没有模型的表中检索值(Rails: retrieve values from a table without a model)

在rails中有没有办法从没有任何模型的表中检索值? 我创建了一个表item_types,其中包含字段name和item_id,其中item_id是另一个表项的主键。 items表中的每个项目可以有3个或4个item_types。 在这里,我想显示具有特定item_id的项目的相应item_types。 我们只是在变量@ item.id中有一个特定项的item_id我试过形成一个sql查询,但我不知道如何在没有模型的情况下实现它。 此外,我不确定下面查询的'item_id = @ item.id'部分是否正确。

select * from item_types where item_id = @item.id

In rails is there any way to retrieve values from a table which does not have any model? I have created a table item_types with the fields name and item_id in which item_id is the primary key of another table items. Each item in items table can have 3 or 4 item_types. Here I want to display the corresponding item_types of an item with a particular item_id. We just have the item_id of a specific item in a variable @item.id I have tried forming an sql query, but I don't know how to implement it without a model. Also I am not sure whether the 'item_id = @item.id' part of the query below is correct.

select * from item_types where item_id = @item.id

最满意答案

您正在查看的是join_model ,它可以与has_and_belongs_to_many或has_many :through关系一起使用

我不知道你想要实现什么,但我强烈建议在你的独立模型中使用has_many :through association:

#app/models/type.rb Class Type < ActiveRecord::Base has_many :item_types has_many :items, through: :item_types end #app/models/item.rb Class Item < ActiveRecord::Base has_many :item_types has_many :types, through: :item_types end #app/models/item_type.rb Class Type < ActiveRecord::Base belongs_to :item belongs_to :type end

这将允许您通过其他模型调用join_model ,如下所示:

@item = Item.find(params[:id]) @item.item_types #-> calls SQL "select * from item_types where item_id = @item.id"

这是正确的Rails方法。 HABTM不使用模型; HMT确实如此。 您应该使用@Bharat Soni's评论生成模型并使用我的代码段访问它

What you're looking at is a join_model, which can either be used with a has_and_belongs_to_many, or has_many :through relationship

I don't know what you're trying to achieve, but I would highly recommend employing a has_many :through association in your independent models:

#app/models/type.rb Class Type < ActiveRecord::Base has_many :item_types has_many :items, through: :item_types end #app/models/item.rb Class Item < ActiveRecord::Base has_many :item_types has_many :types, through: :item_types end #app/models/item_type.rb Class Type < ActiveRecord::Base belongs_to :item belongs_to :type end

This will allow you to call the join_model through your other models, like this:

@item = Item.find(params[:id]) @item.item_types #-> calls SQL "select * from item_types where item_id = @item.id"

This is the correct Rails way to do this. HABTM doesn't use a model; HMT does. You should generate a model using @Bharat Soni's comment & access it with my snippet

更多推荐

本文发布于:2023-07-23 00:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225138.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   retrieve   Rails   model   table

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!