Tag: association

has_one:through => multiple

出席和担保: belongs_to :event belongs_to :account 因此:参加和保证之间的关系是1比1。 没有我太多的想法,有没有办法做到这一点? # attendment has_one :vouching :through => [:event, :account] 注意:实际上,我不介意想太多。

评估:依赖=>:销毁

在Rails 2.2.2(ruby 1.8.7-p72)中,我想评估在实际执行之前销毁对象的影响。 即我希望能够生成一个受以下因素影响的所有对象的列表:dependent =>:destroy(通过对象的关联)。 我试图解决的真正问题是向用户提供将要删除的所有内容的列表,并让他们确认操作。 任何人都可以推荐一个好方法来解决这个问题吗? 我刚刚开始研究ActiveRecord :: Associations,但我没有取得多大进展。 更新:在我的特定情况下,我有各种级别的对象(A – > B – > C)。

数据建模3路表has_many关联

我正在尝试构建一个表来处理某个广告系列已使用以下模型关联设置的位置和类别: class Campaign < ActiveRecord::Base has_many :campaign_category_metro_bids, dependent: :destroy has_many :metros, through: :campaign_category_metro_bids has_many :categories, through: :campaign_category_metro_bids end class Metro < ActiveRecord::Base has_many :campaign_category_metro_bids has_many :campaigns, through: :campaign_category_metro_bids has_many :categories, through: :campaign_category_metro_bids end class Category < ActiveRecord::Base has_many :campaign_category_metro_bids has_many :campaigns, through: :campaign_category_metro_bids has_many :metros, through: :campaign_category_metro_bids end class CampaignCategoryMetroBid < ActiveRecord::Base belongs_to :campaign belongs_to […]

ActiveRecord:定义关联时,将类而不是字符串传递给class_name

在定义关联时,是否存在传递类而不是字符串的含义或陷阱? belongs_to :owner, class_name: User 相反: belongs_to :owner, class_name: “User”

Rails模型关联,has_many:through和has_one具有相同的模型

我有两个模型:用户和状态。 州模型记录了美国50个州中的每个州。 我希望每个用户都有两个属性:一个“主页”状态,以及许多“访问”状态。 我知道我必须建立某种模型关联来实现这一目标,但不确定何时采用最佳方法。 这是我到目前为止所拥有的,但我知道将has_many和has_one关联到同一模型一定有问题。 #user.rb class User :visits has_one :state end #visit.rb class Visit < ActiveRecord::Base belongs_to :user belongs_to :state end #state.rb class State :visits belongs_to :user end 有什么建议?

保存主对象时ActiveRecord是否保存belongs_to关联?

如果我有两个型号: class Post < ActiveRecord::Base belongs_to :user end 和 class User < ActiveRecord::Base has_many :posts end 如果我做: post = Post.new user = User.new post.user = user post.save 用户是否也得到了保存,并且在post的user_id字段中正确分配了主键?

是否可以只询问ActiveRecord关联中的某些列?

考虑 def Foo has_one :user end 假设我只想要一个Foo的User名,而不是任何其他列。 所以我想要 SELECT name FROM “users” WHERE “prices”.”id” = 123 但是做foo.user.name会给我 SELECT * FROM “users” WHERE “prices”.”id” = 123 是否有任何光滑的方式使用该关联只获得一列? 如果没有,那么我必须这样做: User.where(id: foo.user_id).pluck(:name).first

为什么accept_nested_attributes_for allow_destroy不起作用?

我有一个嵌套的属性/资源,我正试图破坏。 我想要做的是在销毁关联的life_hack文章对象时销毁审阅对象。 现在我不太确定accepted_as_nested_attributes是如何工作的,但我很确定这是我需要这样做的方式。 下面我有一些定义如此的路线: Hacklife::Application.routes.draw do devise_for :users root ‘life_hacks#index’ resources :life_hacks, only: [:new, :index, :create, :show, :destroy] do resources :reviews, only: [:new, :create] end resources :reviews, only: [:show] do resources :comments, only: [:new, :create] end 以下是我的LifeHack文章模型: class LifeHack < ActiveRecord::Base validates :title, presence: true validates :content, presence: true belongs_to :user has_many :reviews, dependent: :destroy […]

在视图中显示关联模型的属性

我想获取在博客应用程序中创建文章的用户的用户名或电子邮件(都在用户表中)。 目前,我可以从articles_controller.rb获取用户ID def create @article = Article.new(params[:article]) @article.user_id = current_user.id @article.save redirect_to article_path(@article) end 但不知道如何获取用户名或电子邮件。 基本上我想在文章索引页面上显示用户名或电子邮件。 请建议我如何完成它 user.rb class User < ActiveRecord::Base has_many :articles has_many :comments # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes […]

belongs_to belongs_to association only only has_many或has_one

你能在Rails中拥有belongs_to belongs_to关系吗? 搜索 结果给了我两个结果。 事实上我找不到关于这个主题的非常多的信息,这似乎表明它不应该被做或者是不好的做法。 我昨天问了一个关于a_many关系的问题,但是因为我找不到相关的信息,我会生成一个问题,以便人们在将来更容易搜索这个问题。 我正在解释另一个用户的答案(我希望这没关系)。 鞋子可以有很多袜子,但只有一个活跃的袜子。 其他袜子不活跃。 所有袜子都是独一无二的独特图案。 我想确保我没有相同图案的袜子。 我想我可以这三种方式做到这一点 class Sock < ActiveRecord::Base belongs_to :shoe end 要确定袜子是活动还是非活动,请给它的’所有者鞋提供对其活动袜子的引用,如下所示: class Shoe < ActiveRecord::Base belongs_to :sock end 去它的主人Shoe并检查Shoe的活动袜子是否是当前的袜子。 例如 def is_active owner_shoe.active_sock == self 将它们与外键相关联 class CreateGettingDressed < ActiveRecord::Migration def change create_table :shoes do |t| t.belongs_to :active_sock, foreign_key: "sock_id" t.string :size t.timestamps null: false end […]