无法在Rails应用程序中查找和显示关系模型中的记录列表
我有一个关系模型, has_many :through
关联跟踪has_many :through
中商店的客户(用户)。 我在使用ShopsController
遇到问题并查找给定商店的关系列表,然后显示它们。
我的错误是RecordNotFound, Couldn't find Relationship with 'id'=
所以我遇到了如何创建和识别关系的问题。 当商店登录时,需要根据user_id
找到它们(然后给出current_shop_id
)我如何重新设计这些以使关系/展示工作?
在当前版本中,商店通过关系拥有多个用户:
class Shop < ActiveRecord::Base has_many :relationships has_many :users, through: :relationships
Relationships获取用户的id和shop的id并创建一个关系id:
schema.rb
create_table "relationships", force: :cascade do |t| t.integer "user_id" t.integer "shop_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "relationships", ["shop_id"], name: "index_relationships_on_shop_id" add_index "relationships", ["user_id", "shop_id"], name: "index_relationships_on_user_id_and_shop_id", unique: true add_index "relationships", ["user_id"], name: "index_relationships_on_user_id"
在商店模型中添加了一个用户:
# New relationship def add(user) relationships.create(user.id) end # Unfollows a user. def remove(user) relationships.find_by(user.id).destroy end
@relationships在店铺控制器中定义:
def relationships @title = "Following" @relationships = Relationship.find(params[:relationship_id]) @users = @shop.relationships.paginate(page: params[:page]) render 'show_relationships' end
并且在关系控制器中创建关系
def create user = User.find(params[user_id: user.id]) current_shop.add(user) redirect_to user end
然后这些关系应该显示在人际关系/展示中
控制器中的relationships
方法遍布各处。
你试图找到关系S使用id by id和不存在的params[:relationship_id]
,这会导致你看到的错误。
然后你将@users
设置为@shop
所有关系。
然后你要渲染一个模板show_relationships
但你稍后会提到一个relationships/show
模板。
此外,在商店模型中,您只使用用户ID调用关系create
,而您希望传递关系的某些属性。
当你试图解决这个问题时,看起来这段代码变得更加混乱和混乱。 说实话,我会回到开始并重新开始。
如果我了解您的用例,商店有很多客户,而且客户经常光顾许多商店。
执行此操作的结果是Rails方式是您可以通过使用该商店的实例并调用shop.customers
来检索属于特定商店的客户。您可以通过调用shop.customers.find(:id)
找到该商店的特定客户shop.customers.find(:id)
如果您尝试呼叫其他商店客户,则会收到ActiveRecord::RecordNotFoundError
反之亦然。 要检索客户经常光顾的商店,理想情况下,您需要如上所述的customer.shops
等。 要向客户添加新商店,请使用customer.shops.create("Arkwrights Dairy")
正如沙德威尔所说,你目前创造这种结构的努力远比你需要的复杂得多。 并且您不通过控制器设置它,只能通过模型。
型号,仅限2
class Shop < ActiveRecord::Base has_and_belongs_to_many :customers class Customer < ActiveRecord::Base has_and_belongs_to_many :shops
迁移,3
class SampleMigration < ActiveRecord::Migration def change create_table :shops do |t| t.string :name end create_table :customers do |t| t.string :name end create_table :customers_shops, id: false do |t| t.belongs_to :customer t.belongs_to :shop end end end
根据需要为模型添加更多细节,但这足以建立关系。 值得注意的是复数,商店诗歌商店很重要。 购物课和belongs_to:商店都是单数。 许多商店的参考是复数,桌子有很多商店,所以也是复数。 连接表必须按字母顺序排列,customers_shops不是shop_customers。
设置完成后,运行迁移然后尝试。
shop = Shop.create(name: "Dairy") => # shop.customers.create(name: "Bill") => # customer = Customer.find(1) => # customer.shops => [#]
请注意,商店会返回一个数组。 使用customer.shops [0]或者customer.shops.find(1)访问第一个元素以使用其:id检索商店
希望这能为您提供所需的基础:-)
……对于一个特定的商店……
这表明您应该找到Shop的实例,并使用关联来检索相应的用户。 我期望在以商店为导向的视图中看到这一点,而不是关系视图,并且在Shop上有一个关联:
has_many :users, through => :relationships
- MySQL将UTF-8更改为ASCII-8BIT
- Rails上的UTF8 MySQL问题 – 使用utf8_general_ci编码问题
- 将数据从excelfile写入数据库时编码错误(mysql)
- 迁移创建表引发Mysql2 ::错误:表不存在
- 带有Postgres数据的Rails不按顺序返回
- 使用mysql2 gem获取最后插入的id
- heroku rake db:migrate ERROR“gem install activerecord-mysql-adapter”
- 请求卡在ActiveRecord :: QueryCache中间件中
- 使用Ruby / Chef Recipe for Vagrant导入Mysql数据库