Tag: association

通过关联的新模型对象

我认为可以通过关联创建一个新的模型对象。 class Order < ActiveRecord::Base belongs_to :basket end class Basket < ActiveRecord::Base has_one :order end order = Order.new() basket = order.basket.new() # NoMethodError: undefined method `new' for nil:NilClass

在Rails has_many中保存关联记录的顺序:通过关联

我正在开发一个Rails插件,其中包含一种修改has_many:关联中相关记录顺序的方法。 假设我们有以下型号: class Playlist :destroy has_many :songs, :through => :playlists_songs end class Song :destroy has_many :playlists, :through => :playlists_songs end class PlaylistsSong < ActiveRecord::Base belongs_to :playlist belongs_to :song end 如果我们改变播放列表歌曲的顺序(例如@playlist.songs.rotate! ),Rails不会触及playlists_songs表中的记录(我正在使用Rails 3.1),这是有道理的。 我想调用播放列表的歌曲=方法保存歌曲的顺序,但是,可以通过删除播放列表中的相关现有行并以正确的顺序创建新的行(以便:order => “id”可以在检索它们时使用)或者通过向playlists_songs添加sort:integer列并相应地更新这些值。 我没有看到任何允许这样做的回调(例如before_add)。 在ActiveRecord :: Associations :: CollectionAssociation中 ,相关的方法似乎是writer , replace和replace_records ,但我对最好的下一步将会失败。 有没有办法扩展或安全地覆盖这些方法之一,以允许我正在寻找的function(最好只针对特定的关联),或者是否有一个不同的,更好的方法呢?

如何从rails表单中的对象关联提供数据?

我有一个对象,我正在开发一个具有许多属性的控制器。 但是,由于我创建的数据模型,我必须编辑的大多数属性都是通过其他表中的关联保存的。 EG:我的文章通过标签表有标签(其他表中保存了大约20个其他属性)。 该文章通过多态关联等具有许多其他属性。 关联工作得很好,并且可以保存每个属性的多个条目。 但是,有一件事很难将编辑操作简化为控制器。 通常你可以保存@article = Article.find(params [:id])并显示所有属性。 这意味着表单具有服务的当前属性,并且该人员可以进行更改。 但是,对于通过关联保存的内容,这些字段为空。 我有一个shiv解决方案,例如,@ article.tag_list = @ article.tags.map(&:name)。 这使字段现在显示标签。 但是,对每个属性执行此操作会为控制器中的编辑操作添加许多行。 有没有更好的办法? 如果有一些代码我应该发布,我可以 – 只是没有,因为它现在很乱,所以我想我会解释。

Rails + MongoID +简单forms关联:#Mongoid :: Relations :: Metadata的未定义方法`options’

我有以下模型,基本上是指课程和类别。 每节课可以有一个类别,每个类别都嵌入在课程中。 class Lesson include Mongoid::Document field :title, :type => String field :category, :type => String field :price, :type => Integer field :description, :type => String field :user_id, :type => String validates_presence_of :title validates_presence_of :category validates_presence_of :price validates_presence_of :user_id validates_numericality_of :price attr_accessible :title, :category, :description, :price embeds_one :category end class Category include Mongoid::Document field :name, […]

Rails 4,has_many通过关联 – 查找关联的对象

我想知道为什么Item.where(category: x)不适合我。 我希望这个语句能够返回分类在类别x下的所有项目。 请看下面我的协会: class Category :categorizations end class Item :categorizations end class Categorization < ActiveRecord::Base belongs_to :item belongs_to :category end Category.find(1).items返回该类别的所有项目。 请参阅下面的byebug控制台输出,它进一步说明了我的观点。 看起来生成的SQl正在寻找Item表中的类别列 – 当然不存在。 有任何想法吗? 1: class ItemsController 6: end 7: (byebug) Item.where(category: 1) Item Load (0.6ms) SELECT “items”.* FROM “items” WHERE “items”.”category” = 1 # (byebug) Category.find(1).items Category Load (0.7ms) SELECT “categories”.* […]

Rails按关联数据排序

是否可以按教师的名字订购school.classrooms的结果? 我想直接在关联中执行此操作,而不是单独调用。 class School < ActiveRecord::Base has_many :classrooms end class Classroom < ActiveRecord::Base belongs_to :school belongs_to :teacher end class Teacher < ActiveRecord::Base has_one :classroom end

has_many通过与现有对象/ ActiveRecord的关联

给定模型 class Composition :compositions_tags end class Tag :compositions_tags validates_uniqueness_of :tag, only: [:create, :update], message: “already taken” end class CompositionsTag < ActiveRecord::Base belongs_to :composition belongs_to :tag end 现在,例如我 Composition.create(content: “Hello”).tags.create(text: “#hi”) 结果将是具有内容“Hello”的组合和已创建文本“#hi”的标签。 然后我再创一个作文。 Composition.create(content: “Goodmorning”) 现在我不知道和想要做的是将其与文本“#hi”的现有标签相关联。 我该如何以最优雅的方式做到这一点?

Ruby on Rails – 求和计算不与模型中的关联一起使用

我有一个User表,其列total_user_xp与has_many :goals关联。 目标表有一个total_goal_xp列,它使用以下方法在模型中计算: # goal.rb belongs_to :user has_many :goal_activities has_many :activities, through: :goal_activities def total_goal_xp self.total_goal_xp = self.goal_activities.sum(:total_xp) end 出于某种原因,在我的用户控制器和模型文件中,我无法计算user_id与current_user匹配的total_goal_xp的总和。 它总是返回值0,就好像total_goal_xp没有存储在表中一样。 我对rails很新,但我感觉我的total_goal_xp方法没有将值保存到db。 它似乎每次调用该方法时重新计算总和。 也就是说,在rails控制台中,运行Goal.all会给我一个表,它有正确的值,所以我不明白为什么以下方法返回值0。 user_controller.rb def show @user_goals = current_user.goals.all @user_xp = @user_goals.sum(:total_goal_xp) end user.rb has_many :goals def set_total_user_xp goal = self.goals.all self.total_user_xp = goal.sum(:total_goal_xp) end 在服务器日志中,我得到以下SQL – SELECT SUM(“goals”.”total_goal_xp”) FROM “goals” WHERE “goals”.”user_id” = […]

使用mongodb作为数据库的rails中的关联

我正在使用设计。 它将当前用户标识赋予 current_user.id 有很多用户。 控制器名称为empsals_controller.rb class EmpsalsController < ApplicationController def index @empsals = Empsal.all end def show @empsal = Empsal.find(params[:id]) end def new @empsal = Empsal.new end def edit @empsal = Empsal.find(params[:id]) end def create @empsal = Empsal.new(params[:empsal]) respond_to do |format| if @empsal.save format.html { redirect_to @empsal, notice: 'Empsal was successfully created.' } format.json { […]

has_many通过条件不起作用

我有一个模型等级和一个模型用户。 在成绩和用户之间通过协作实现多对多关联。 在user.rb中 has_many :grades, through: :collaborations, source: :user 工作,但我只需要获得属性“archived”= false的成绩 我试过了 has_many :grades, through: :collaborations, source: :user, conditions: [‘ archived = ? ‘, false] 但它需要所有等级,换句话说,条件被忽略。 我可以在这种情况下加入我的合作,但协作与Grade和School有多态关系,而且学校没有存档字段,这些都会导致错误。 有任何想法吗?