Has_many通过在Rails中分配不同的角色 [英] Has_many through in Rails while assigning different roles

查看:63
本文介绍了Has_many通过在Rails中分配不同的角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在地址和旅程之间建立一种关系,但是我不确定如何建立这种关系.

I'm trying to create a relationship between addresses and trips, and I'm not sure exactly how to set up the relationship.

每次旅行都会有两个地址:起始地址和结束地址. 地址可以在许多不同的行程中使用,根据行程,它们可以是起始地址也可以是结束地址. 我设想的是,当用户创建新旅程时,他们可以从所有地址的下拉列表中进行选择,因此他们可以从称为家"的地址到称为机场"的地址进行旅程. "

Each trip will have two addresses: the starting address and the ending address. Addresses can be used in many different trips, and they can either be the starting address or the ending address depending on the trip. What I envision is that when users are creating a new trip, they can select from a dropdown of all their addresses, so they could make a trip from their address called, say, "home" to their address called, say, "airport."

应用程序中的其他模型之间的地址(已定位)之间已经建立了多态关系,但是在这种情况下,相同的地址需要属于两个不同的模型(用户和旅程).多态联接表会是一个好的解决方案吗?即使确实可以解决问题,将跳闸连接到两个不同的地址后,区分起始地址和结束地址的最佳方法是什么?

There is already a polymorphic relationship set up between addresses (as locatable) with some other models in the app, but in this case the same address needs to belong to two different models (the user and the trip). Would a polymorphic join table be a good solution? Even if that did solve the problem, once you had connected two different addresses to trips, what's the best way to distinguish the starting address from the ending address?

谢谢您的建议!

我已经用hakunin实现了下面的所有内容,但是仍然找不到使功能起作用的方法.我决定使用fields_for为每个Trip构建TripLocation对象,但是我不知道要在控制器中放置什么.当我放:

I have implemented everything below by hakunin, but I still cannot find a way to make the feature function. I decided to use fields_for to build the TripLocation objects for each Trip, but I cannot figure out what to put in the controller. When I put:

def new
  @trip = Trip.new
  @trip.origin_trip_location.build
  @trip.destination_trip_location.build
end

我收到错误undefined method build for nil:NilClass.我当时在考虑只使用@trip.trip_location.build,但随后出现错误undefined method trip_locations for #<Trip:0x007f5a847f94b0>,因为在Trip的模型中未显示has_many :trip_locations.通过仅使用常规has_many :trip_locations 我已经能够仅使用表单帮助器fields_for :trip_locations并说一个Trip has_many :trip_locations来将所有必要的信息输入到联接表中,但是然后我没有方法来查询和查找哪个地址在联接中具有布尔值将该表设置为true,并将哪个表设置为false.我想,如果我能解决这个问题,我会万事俱备的.

I get the error undefined method build for nil:NilClass. I was thinking of using just @trip.trip_location.build instead but then I get the error undefined method trip_locations for #<Trip:0x007f5a847f94b0> because in the model for Trip does not say has_many :trip_locations. By just using regular has_many :trip_locations I have been able to enter in all the information necessary into the join table just using the form helper fields_for :trip_locations and saying that a Trip has_many :trip_locations, but then I have no method to query and find which address has the boolean in the join table set as true and which one has it set as false. If I could just get that problem solved, I would be all set, I think.

推荐答案

在rails中,这通常是在关联条件下完成的.您可以将其与"has_one through"结合使用.创建一个新模型,我们将其称为TripLocation,这将是行程和地址之间的映射表.在其中您将有该列,说目的地".如果该列为true,则此映射用于目标地址.

In rails this is normally done with a condition on the association. You can use it in combination with "has_one through". Create a new model, let's call it TripLocation which would be the mapping table between trips and addresses. In it you would have the column, say "destination". If the column is true, this mapping is for a destination address.

所以我们说迁移看起来像这样:

So let's say the migration looks like this:

create_table :trip_locations do |t|
  t.belongs_to :trip
  t.belongs_to :address
  t.boolean :destination
end

这些就是模型:

class TripLocation < ActiveRecord::Base
  belongs_to :trip
  belongs_to :address
end

class Trip < ActiveRecord::Base    
  has_one :origin_trip_location,
    class_name: 'TripLocation',
    conditions: { destination: nil }

  has_one :destination_trip_location,
    class_name: 'TripLocation',
    conditions: { destination: true }

  has_one :origin, through: origin_trip_location, source: :trip
  has_one :destination, through: destination_trip_location, source: :trip
end

因此,由于在通过"关联中设置的条件,调用@trip.origin@trip.destination应该会为您提供正确的地址.

So because of the conditions set on the "through" associations, calling @trip.origin and @trip.destination should give you the correct addresses.

在将地址指定为始发地或目的地时,您可以简单地将地址分配给所需的地址. @trip.origin = Address.first@trip.destination = Address.second,我相信它应该通过设置目标标志来做正确的事情.试试吧.

When it comes to designating addresses as origin or destination, you can simply assign an address to whichever you need. @trip.origin = Address.first, or @trip.destination = Address.second, and I believe it should do the right thing with setting the destination flag. Try it.

这篇关于Has_many通过在Rails中分配不同的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆