Tag: activerecord

Ruby on Rails – 创建新条目时,使用回调将两个对象在单独的表中相乘

我有3个表,目标,活动和Goal_Activities。 目标有一个xp的设定值,而Activity接受来自用户的输入。 使用Activity中的on_create方法,我想将用户输入与目标xp相乘并将其存储在目标活动表中,这样我就可以保留每个更新的记录,并最终将它们全部添加到一起以创建总目标xp in目标表。 我在SO, 这里和这里找到了几个关于类似主题的问题,但我发现很难理解模型文件之间的关联以及如何访问不同模型中的某些方法。 我还阅读了关于回调的文档,我认为这是我需要设置的,但如果有更好的方法,请告诉我。 这是我目前的模特协会。 goal.rb class Goal < ActiveRecord::Base has_many :goal_activities has_many :activities, through: :goal_activities end goal_activity.rb class GoalActivity < ActiveRecord::Base belongs_to :goal belongs_to :activity end activity.rb class Activity < ActiveRecord::Base has_many :goal_activities belongs_to :goal, through: :goal_activities end 以下是表中的一组示例值。 目标 goal_id:1,xp:500,total_goal_xp:默认值为0 活动 activity_id:1,数量:3,goal_id:1 Goal_activities goal_activity_id:1,goal_id:1,activity_id:1,total_xp:1500 一旦创建了活动,total_xp就会被添加到目标表中的total_goal_xp中。 任何意见,将不胜感激。 如果您有任何疑问,请告诉我。

为什么此ActiveRecord语句在Server中失败但在Console中失败

我有以下查询在特定控制器中的rails s中失败。 在rails c没有失败。 Post.includes(:user).find(:all, :limit => 10, :order => ‘users.display_name ASC’) 在控制台中,它返回正确的数据。 在服务器中,我收到此错误 ActiveRecord::StatementInvalid: PGError: ERROR: column posts.users.display_name does not exist LINE 1: …s_count” AS t0_r7, “posts”.”popularity” AS t0_r8, “posts”.”u… ^ 查询很长,我只想包含一些相关的片段 : SELECT “posts”.”id” AS t0_r0, “posts”.”post_content” AS t0_r1, … “posts”.”popularity” AS t0_r8, “posts”.”users.display_name” AS t0_r9, “posts”.”subtopics.name” AS t0_r10, “posts”.”categories.category_name” AS t0_r11 … […]

为什么组计算字段不会显示在查询结果中?

我有这样的查询: query = Link.select(‘url, max(created_at) as created_at, count(*) as url_count’).group(:url).order(‘url_count desc, created_at asc’) query.results.first示例结果: 2.2.0 :002 > query.first => # 为什么这里没有url_count ,即使我知道它是。 2.2.0 :003 > query.first.url_count => 17

Has_many通过:关联不在Rails 5.2中注册

所以在这个问题上,我有一个has_and_belongs_to_many在people occasions之间使用连接表。 我有它工作(在帮助我的可爱SO向导的帮助下),但说向导建议has_many through: :association可能更适合我的目的。 不幸的是,它确实存在,但在转移过程中我似乎打破了它。 我的schema.rb现在是这样的,使用gifts连接people occasions : create_table “gifts”, force: :cascade do |t| t.string “name” t.decimal “price”, precision: 8, scale: 2 t.string “store” t.text “notes” t.boolean “purchased”, default: false t.integer “user_id” t.integer “person_id”, null: false t.integer “occasion_id”, null: false t.datetime “created_at”, null: false t.datetime “updated_at”, null: false t.index [“occasion_id”], name: “index_gifts_on_occasion_id” t.index [“person_id”], […]

NameError:has_many通过关系的未初始化常量

我似乎无法做到这一点。 我有许多通过关系只是不工作。 这是设置: class Group :destroy has_many :phone_numbers, through: :groups_phone_numbers attr_accessible :name end class PhoneNumber < ActiveRecord::Base belongs_to :user has_many :responses has_many :groups_phone_numbers has_many :groups, through: :groups_phone_numbers attr_accessible :label, :number end class GroupPhoneNumber < ActiveRecord::Base belongs_to :group belongs_to :phone_number end 我已经尝试了复数化的每个变体,只是无法超越未初始化的错误。 我究竟做错了什么? 数据库中的表(连接模型)称为groups_phone_numbers。 确切的错误(g是一组): 1.9.3p0 :002 > p g.phone_numbers NameError: uninitialized constant Group::GroupsPhoneNumber 进行连接表的迁移: class […]

ActiveRecord where.not不工作/奇怪的行为

用户has_many计划。 我正在尝试查找没有状态为“已取消”的所有用户的ID。 很想知道下面的行为是什么。 对于上下文,应该返回的是: User.select { |u| u.plans.select { |p| p.status != “canceled” }.count > 0 }.map(&:id) # => [27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 41, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 61, 62, 63] 这是我得到的: # statement 1 […]

Rails ActiveRecord:如何通过超级父视图获取非重复的子记录?

我有四张名为Albums , Songs , Singers和SongSingers 。 Albums { Id, Name } Songs { Id, AlbumId, Name } Singers { Id, Name } SongSingers { Id, SongId, SingerId } 专辑有很多歌。 歌曲属于专辑。 SongSinger属于Song and Singer。 歌曲和歌手有许多SongSingers。 每首歌可能有相同或不同的歌手。 在View中,如何通过AlbumId获取所有非重复的歌手。 谢谢。

SQL查询顺序问题

所以我在activerecord中有以下模型,我将使用Discussion.user_replied_group_discussions(@user)它返回最近回复的讨论好,但是如何修改私有方法以按最新回复时间订购返回的讨论? # Table name: discussions # # id :integer not null, primary key # title :string(255) not null # content :text(255) not null # created_at :datetime # updated_at :datetime # user_id :integer # discussable_id :integer # discussable_type :string(255) class Discussion :commentable, :dependent => :destroy # #this is the scope that I am having trouble with: […]

在RoR中将简单查询转换为棘手的命名范围

我有一个简单的范围问题。 我想这样做作为一个范围: if article.responses.blank? return false elsif article.responses.last.passed.eql?(false) return true else return false end 所以在文章模型上我会有这样的事情: scope :failed_response, { :joins=>[:responses], :conditions=>[“responses.passed = ?”, false] } 问题是,我只想要最近响应失败的实例。 我确信这些是用花式排序或某种嵌套查询来做到这一点的方法,但我被卡住了。 谢谢!

通过关系更新rails has_many

我有模特 class Agency < ActiveRecord::Base has_many :specializations has_many :cruise_lines, through: :specializations end class CruiseLine < ActiveRecord::Base has_many :specializations has_many :agencies, through: :specializations end class Specialization < ActiveRecord::Base belongs_to :agency, inverse_of: :specializations belongs_to :cruise_line, inverse_of: :specializations end 我想更新Specialization集合(即删除一些旧的关系,并在需要时添加一些新的)。 我应该更新关系的方法看起来像这样(在一些单独的服务中): def self.update_agency_specializations(agency, params) attributes = params.require(:agency).permit( { cruise_line_ids: [] } ) attributes[:cruise_line_ids].select{ |x| x.to_i > 0 }.each […]