Tag: activerecord

为什么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 […]

rails有效记录查询一起列出的2个属性

我想创建一个索引任务站点,并希望在同一列表中列出所有任务(模型中的传入名为execu_tasks和模型中的传出名为assigned_tasks),基于created_at进行混合。 使用以下代码,我可以为传入和传出任务创建彼此之间的2个单独列表,但看起来不太好。 我甚至不确定是否应该在模型,控制器或视图中进行一些更改。 task.rb belongs_to :assigner, class_name: “User” belongs_to :executor, class_name: “User” user.rb has_many :assigned_tasks, class_name: “Task”, foreign_key: “assigner_id” has_many :executed_tasks, class_name: “Task”, foreign_key: “executor_id” tasks.controller.rb def index @user = current_user @assigned_tasks = @user.assigned_tasks.order(“created_at DESC”) @executed_tasks = @user.executed_tasks.order(“created_at DESC”) end 任务/ index.html.erb

访问params 值内的参数数组

我有以下参数: Parameters: {“utf8″=>”✓”, “authenticity_token”=>”06Us9R1wCPCJsD06TN7KIV/2ZeH4dJlZVqc12gpKBbo=”, “run”=>{“box_id”=>”1”}, “tapes”=>{“1″=>{“tape_id”=>”1”}, “2”=>{“tape_id”=>”2”}, “3”=>{“tape_id”=>”3”}}, “commit”=>”Create Run”}} 我想创建一个框ID为1的新“运行”,然后将磁带1 2和3关联到此运行,框ID为1 我不确定需要进入控制器的代码,我尝试过: def create @run = Run.new(params[:run]) @tape_ids = @run.build_tape(params[:run][:tapes]) @run.save 当我提交下面的表单时,它会使用正确的框创建一个新的Run,但不会将磁带与它相关联。 prohibited this tape from being saved: index%> index %>

ActiveRecord触摸导致死锁

我的应用程序广泛使用touch ,以利用Rails的模板缓存系统。 当批量中的许多不同对象之间创建了许多关系时,我的应用程序会执行某种类型的工作。 有时,某些工作导致产生的级联touch导致死锁。 对于我经常看到它的一种情况,我可以围绕这个进行编码,但是看到它已经揭示了更大的问题,这可能发生在其他情况下,尽管它不太可能发生。 要理解这一点,请考虑两个人在Twitter上完全相同的时刻。 它们都单击“跟随”,导致在它们之间创建关系对象,然后touch它们的每个记录。 如果这些接触成为交织: 进程1触及用户A. 进程2触及用户B. 进程1触及用户B. 进程2触及用户A. 每个进程都使用数据库事务,因此这将导致死锁。 我错了,这可能发生在我奇怪的批处理作业场景之外的正常应用程序操作中吗? 如果我没错,有什么办法吗? 我可以以某种方式将touch移动到交易之外吗? (最后写的胜利无论如何都可以更新updated_at …) 更新 – 更多数据模型的解释 class Follow belongs_to :follower, touch: true belongs_to :followee, touch: true end @u1 = User.find(1) @u2 = User.find(2) # Background Job 1 Follow.create!(follower: @u1, followee: @u2) # Background Job 2 Follow.create!(follower: @u2, followee: @u1)

rails + ActiveRecord:缓存模型的所有寄存器

我有一个小模型(让我们称之为“节点”)代表树状结构。 每个节点只包含一个名称和对其父亲的引用: class Node < ActiveRecord::Base validates_presence_of :name, :parent_id end 表格不是很大 – 少于100个元素。 它很少更新 – 在过去4个月中,网站管理员一次添加了20个新元素。 然而,它在我的应用程序中使用了很多。 鉴于其树状结构,在某些情况下,请求会触发超过30个数据库命中(包括ajax调用,我使用了很多)。 我想使用某种缓存来降低数据库访问 – 因为表太小了,我想到缓存内存中的所有寄存器。 这可能是轨道2.3吗? 有没有更好的方法来处理这个?

MySQL:删除连续的重复值

我有一个MySQL表,返回包含连续重复项的值列表(按时间戳排序)。 例如,在查询时,我只需要返回连续重复的值: [1, “Yellow”] [2, “Yellow”] [3, “Green”] [5, “Black”] [6, “Green”] [7, “Green”] 这里的数字用于参考 – 该值实际上是字符串“Green”,因此对于上述情况,新的未列出的列表将是: [1, “Yellow”] [3, “Green”] [5, “Black”] [6, “Green”] 有没有一种聪明的方法来处理MySQL的这个问题? 使用Rails / ActiveRecord,而不是那应该有所作为,但我可以通过操作数组来做到这一点,只是想知道是否有更聪明的方法来处理它。

Rails 4 MySQL bigInt主键问题和错误

我需要在rails 4.1.8应用程序中使用14位bigInt作为主键。 使用SO上的旧post作为指导,我想出了以下内容来解决这个问题…… class CreateAcctTransactions false do |t| t.integer :id, :limit => 8,null: false t.integer “account_id”,limit: 8,null: false t.integer “transaction_type_id”, null: false t.datetime “date”,null: false t.text “description”,limit: 255 t.decimal “amount”,precision: 10, scale: 2, null: false end end end 但是,这种方法并没有真正将“id”指定为主键 ,它只是另一个普通字段。 此外,当我收到以下错误时…… Mysql2 ::错误:字段’id’没有默认值:INSERT INTO acct_transactions ( account_id , amount , date , description , transaction_type_id […]

rails activerecord,friend relation + inverse_friend关系如何获得相互关系? 代码包括在内

试图找到相互关系,在朋友关系中,已经有朋友和inverse_friends。 但如何将它们结合起来以获得共同的朋友呢? 似乎无法弄清楚我尝试了几个选项并在网上搜索了很长时间,只是看不到它 has_many :friendships has_many :friends, :through => :friendships has_many :inverse_friendships, :class_name => “Friendship”, :foreign_key => “friend_id” has_many :inverse_friends, :through => :inverse_friendships, :source => :user 怎么得到一个 has_many :mutual_friends ?

ActiveRecord:如何将属性值插入自定义validation错误消息?

我有一个validates_presence_of :pricevalidation,当validation失败时返回”Book version price can’t be blank”错误。 class BookVersion < ActiveRecord::Base attr_accessible :name, :book_id, :instock, :isbn, :price, :famis_price, :famis_number, :weight_in_pounds, :width, :height, :thickness validates_presence_of :price, message: "can't be blank" 但是,我想将BookVersion的name值添加到validation消息中,如下所示: 如果book_version.name返回“LB”: Book version LB price can’t be blank 所以我想做的事情如下: validates_presence_of :price, message: “#{self.name} can’t be blank” 但那就是返回Book version price BookVersion can’t be blank ,这不是我想要的

如何加载多态模型?

我正在尝试优化我们的一些查询。 一个特别的挑战是试图加载多态模型。 在这种情况下, UserView是多态的,并具有以下列: user_id, user_viewable_id, user_viewable_type 当我尝试运行此查询时。 @user_views = UserView.select(‘user_views.* AS user_views, songs.* AS songs, users.* AS users’) .joins(‘INNER JOIN users AS users ON user_views.user_id = users.id’) .joins(‘INNER JOIN songs AS songs ON user_views.user_viewable_id = songs.id’) .where(:user_viewable_type => ‘Song’).order(‘user_views.id DESC’).limit(5) 它似乎并不急于加载查询。 我正在使用名为MiniProfiler的gem,它表示它实际上运行的是n + 1个查询,而不是一个。 以下AR查询返回此SQL: SELECT user_views.* AS user_views, songs.* AS songs, users.* AS […]