我有一个关系模型,用于跟踪has_many :through
关联中商店的客户(用户)。我的ShopsController
在查找给定商店的关系列表,然后显示它们时遇到问题。
我的错误是RecordNotFound,Couldn't find Relationship with 'id'=
,所以我在如何创建和识别关系方面遇到了问题。当商店登录时,需要根据user_id
找到它们(然后给出current_shop_id
)我如何重新处理它以使关系/展示发挥作用?
在当前版本中,商店通过关系拥有许多用户(_M):
class Shop < ActiveRecord::Base
has_many :relationships
has_many :users, through: :relationships
Relationships获取用户的id和商店的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
@关系在车间控制器中定义:
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
然后这些关系应显示在关系/显示中
<div class="col-md-8">
<h3><%= @title %></h3>
<% if @relationships.any? %>
<ul class="users follow">
<%= render @relationships %>
</ul>
<% end %>
</div>
控制器中的relationships
方法无处不在。
您正试图使用不存在params[:relationship_id]
的find-by-id查找关系S,这导致了您看到的错误。
然后将@users
设置为@shop
的所有关系。
然后,您将渲染一个模板show_relationships
,但稍后将参考一个relationships/show
模板。
此外,在商店模型中,您对只有用户id的关系调用create
,而您希望传递关系的一些属性。
在您试图解决问题的过程中,这段代码似乎变得越来越混乱。老实说,我会回到最初,重新开始。
如果我理解您的用例,那么一个商店有很多客户,而一个客户经常光顾很多商店。
使用Rails方法这样做的结果是,只需使用特定商店的实例并调用shop.customers
,就可以检索属于该商店的客户。您可以通过调用shop.customers.find(:id)
找到该商店的特定客户。如果您尝试调用另一个商店客户,则会得到ActiveRecord::RecordNotFoundError
反之亦然。要检索客户经常光顾的商店,理想情况下您需要如上所述的customer.shops
等。要向客户添加新店,请使用customer.shops.create("Arkwrights Dairy")
正如Shadwell所说,你目前创造结构的努力远比你需要的复杂。并且,您不会通过控制器来设置,而是通过模型来设置。
型号,仅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
在您认为合适的时候为模型添加更多的细节,但这应该足以建立关系。值得关注的是复数,商店诗商店很重要。Shop the class和belongs_to:Shop都是单数。提及许多商店是复数,桌子容纳许多商店,因此也是复数。联接表必须按字母顺序排列,customer_shops而不是shops_customers。
设置好后,运行迁移,然后尝试。
shop = Shop.create(name: "Dairy")
=> #<Shop id:1, name: "Dairy">
shop.customers.create(name: "Bill")
=> #<Customer id:1, name: "Bill">
customer = Customer.find(1)
=> #<Customer id:1, name: "Bill">
customer.shops
=> [#<Shop id:1, name: "Dairy">]
请注意,商店返回一个数组。使用customer.stores[0]或customer.stores.find(1)访问第一个元素,以使用其:id 检索商店
希望这将为您提供所需的基础:-)
对于给定的商店。。。
这表明您应该找到Shop的一个实例,并使用关联从中检索相应的用户。我希望在面向商店的视图中看到这一点,而不是关系视图,并且在商店上有一个关联:
has_many :users, through => :relationships