Tag: association

Shoulda Matcher和has_many通过:未定义的方法`class_name’为nil:NilClass

我试图通过与Rspec和Shoulda匹配器的关系测试a:has_many。 # student.rb has_many :presences has_many :calls, through: :presences # student_spec.rb it { should have_many(:presences) } it { should have_many(:calls).through(:presences) } #presence.rb belongs_to :call belongs_to :student #presence_spec.rb it { should belong_to(:call) } it { should belong_to(:student) } #call.rb has_many :presences has_many :students, through: :presences #call_spec.rb it { should have_many(:presences) } it { should have_many(:students).through(:presences) } […]

Rails STI和has_many通过关联不起作用,SQLException:没有这样的表

我有以下型号: class Test < ApplicationRecord end class Exam < Test end class Practice < Test has_many :relations has_many :questions, through: :relations end class Relation < ApplicationRecord belongs_to :practice belongs_to :question end class Question < ApplicationRecord has_many :relations has_many :practices, through: :relations end 这是我的架构: create_table “questions”, force: :cascade do |t| t.string “title” t.text “text” t.datetime “created_at”, […]

使用rails 3以内部forms更新嵌套表单和现有数据

我正在尝试使用嵌套的表单视图来正确更新。 然而,当第二种forms具有现有数据时,这会引起问题。 我正在使用accepts_nested_attributes_for和nested_form_for。 第二个目的是使用js动态添加表单元素。 有关更多信息,请参阅github 我得到的错误是: Couldn’t find Muscle with ID=3685340 for Exercise with ID=212831413 我试图手动进行更新,但我的代码并没有真正起作用,我的印象是它不应该被需要因为rails假设在引擎盖下处理它。 这个想法是:练习通过目标有很多肌肉,而且在练习forms中,我希望能够增加目标肌肉。 我的模特: class Exercise :destroy has_many :muscles, :through => :targets accepts_nested_attributes_for :muscles, :reject_if => :all_blank … end class Target true belongs_to :muscle end class Muscle :destroy has_many :exercises, :through => :targets end 我的(haml)观点: %p %b Target(s): = f.fields_for :muscles […]

有没有办法使用AREL进行自定义关联?

model Post # ActiveRecord associations have tons of options that let # you do just about anything like: has_many :comments has_many :spam_comments, :conditions => [‘spammy = ?’, true] # In Rails 3, named scopes are ultra-elegant, and let you do things like: scope :with_comments, joins(:comments) end 有没有办法使用AREL或其他更精简的语法来定义自定义关联,就像命名范围一样优雅? 更新 我认为将这种细节放入一个关联中并不是一个好主意,因为关联应该总是/大多数定义模型之间的基本关系。

Rails :: inverse_of和Association扩展

我有以下设置 class Player :player do def in_hand find_all_by_location(‘hand’) end end end class Card :cards end 这意味着以下工作: p = Player.find(:first) c = p.cards[0] p.score # => 2 c.player.score # => 2 p.score += 1 c.player.score # => 3 c.player.score += 2 p.score # => 5 但以下行为方式不同: p = Player.find(:first) c = p.cards.in_hand[0] p.score # => 2 […]

Rails 4多对多关联不起作用

Ruby on rails新手在这里。 试图创建一个入门博客应用程序,并在我的模型之间的多对多关联中遇到问题。 我有2个模型 – post,类别,彼此之间有多对多关联。 我的问题:当我创建一个新post时,Post会被保存,但是类别后关联不会保存在categories_posts表中。 我的代码如下。 感谢您对此的投入。 post.rb class Post < ActiveRecord::Base validates_presence_of :title, :body, :publish_date belongs_to :user has_and_belongs_to_many :categories end category.rb class Category < ActiveRecord::Base validates_presence_of :name has_and_belongs_to_many :posts end categories_posts.rb class CategoriesPosts < ActiveRecord::Base end 迁移 – create_posts.rb class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :title […]

类别,子类别和律师之间的关联

我有很多律师,类别和子类别。 提示 (如果我的协会合适 ,你可以有一个线索) 在类别表上,我不希望在类别表上看到引用子类别的列。 在子类别表上,我不希望在Subcategories表上看到引用类别的列。 并非所有类别都有子类别。 即一些没有图中所示的子类别。 我有2个单独的表单创建类别和子类别。 我将category_id和subcategory_id作为外键添加到我的律师表中。 这样我就可以在创建时选择律师forms,律师将在图像中尽快选择类别或子类别。 另请注意:对于没有子类别的类别,可以在任何时间,任何一天创建子类别,以及已经具有某些子类别的类别下的新子类别,并且律师将被置于其下。 图像是我目前正在进行的索引/主页的复制品,至少在上面的第6个上面任何时候生效,我希望使用循环来实现这个视图。 图解理解我想要做的事情: 这是我在3个模型之间的关系 class Lawyer < ActiveRecord::Base belongs_to :category belongs_to :subcategory end class Category < ActiveRecord::Base has_many :lawyers end class Subcategory < ActiveRecord::Base #belongs_to :category #Do I want "category_id" in Subcategories Table? has_many :lawyers end 题 我在这3个型号上的关联是否适合我提供的提示? 这非常令人困惑。

Rails 4:如何使用includes()和where()来检索关联对象

我无法弄清楚如何使用.where()方法来检索关联的模型数据。 在此示例中,Projects belongs_to Users … class Project < ActiveRecord::Base belongs_to :user has_many :videos end class User < ActiveRecord::Base has_many :projects end class ProjectsController params[:id]} ).first end end 在App / views / projects / invite.html.erg 返回: — !ruby/object:Project attributes: id: 22 name: Some Project Name belongs_to: 1 instructions: Bla bla bla active: true max_duration: 2 max_videos: […]

外键(class_id)未在belongs_to关联中填充

我是rails的新手,并在rails3(beta4)上构建了一个小型测试应用程序。 我使用Authlogic来管理用户会话( 根据本教程以标准方式设置) 我有脚手架和设置卡模型(基本上是一个post),并为belongs_to和has_many关系设置基本的活动记录关联 user.rb has_many :cards card.rb belongs_to :user 我将我的外键列添加到我的卡表: user_id ,默认为1 一切都很有效 。 我已经完成了我的观点,我可以看到关联,卡可以属于各种用户,这一切都很棒。 但是在创建新卡时,我似乎无法抓住当前的活跃用户 cards_controller.rb def new @card = Card.new @card.user = current_user respond_to do |format| format.html # new.html.erb format.xml { render :xml => @card } end end current_user在应用程序控制器中定义, 可在此处查看 user_id传递NULL并且没有任何内容写入数据库user_id列,使其默认为1而不是实际登录用户的ID。 我尝试过像@user = current_user这样的显而易见的事情 它可能是非常简单的东西,但今天是我第一次使用rails的真实日子 – 谢谢!

检索给定用户评论的所有post,Ruby on Rails

我有用户,post和评论。 用户只能在每个post上发布一条评论。 class User < ActiveRecord::Base has_many :posts has_many :comments end class Post < ActiveRecord::Base has_many :comments belongs_to :user end class Comment < ActiveRecord::Base belongs_to :user belongs_to :post end 在用户页面上(例如, http://host/users/1 )我想显示给定用户评论过的所有post。 然后每个post都会有所有其他评论。 我可以在我的用户控制器中执行以下操作: def show @user = User.find(params[:user_id]) @posts = [] user.comments.each {|comment| @posts << comment.post} end 通过这种方式,我会找到User,然后是他的所有评论,然后是每个评论的相应post,然后(在我看来)每个post我将呈现post.comments。 我是Rails中的新手,所以我可以做到这一点=)但我认为它有点不好而且有更好的方法来做到这一点,也许我应该使用范围或named_scopes(不知道这是什么,但看起来害怕)。 所以你能指出我在正确的方向吗?