activerecord

Nested form validation statements repeating multiple times

痞子三分冷 提交于 2019-12-12 05:49:08
问题 Given the following associations: class Parent has_many :a_childs validates_associated :a_childs accepts_nested_attributes_for :a_childs has_many :b_childs validates_associated :b_childs accepts_nested_attributes_for :b_childs has_many :charges validates_associated :charges accepts_nested_attributes_for :charges validate :test_puts def test_puts puts "validating Parent - #{self.attribute}" end end class AChild belongs_to :parent has_many :charges validates_associated :charges accepts_nested

Rails / Active Record find with join, limit and offset

本秂侑毒 提交于 2019-12-12 05:41:40
问题 I've put together the following query, which works as I expect it to: stuff = @thing.children.find( :all, :joins => :other, :conditions => {:others => {:another_id => some_id}}, :limit => my_limit, :offset => my_offset, ) However, queries of the form find(:all) are deprecated. I have tried changing my query to the following form: stuff = @thing.children.find( :joins => :other, :conditions => {:others => {:another_id => some_id}}, :limit => my_limit, :offset => my_offset, ).all but this throws

Rails, tips for solving this n+1?

不问归期 提交于 2019-12-12 05:38:00
问题 I have an employee view, where are listed all skills, which are written in the skills table on my db. For every employee, all skills are displayed, just as want it to. Employees and skills are related to each other as has many :through association. class Employee < ApplicationRecord has_many :employeeskillsets, foreign_key: "employee_id" has_many :skills, through: :employeeskillsets end class Skill < ApplicationRecord has_many :employeeskillsets, foreign_key: "skill_id" has_many :employees,

CSV read v/s Temp table read from database, optimization of the loop and active record usage . Ruby

倖福魔咒の 提交于 2019-12-12 05:36:07
问题 CSV parsing of the file was very slow so I was trying to load the file directly in to some temp table in database directly and then doing the computation as below : Earlier it was like this, took 13 mins to add the entries using below method : CSV.foreach(fileName) do |line| completePath = line[0] num_of_bps = line[1] completePath = cluster_path+ '/' + completePath inode = FileOrFolder.find_by_fullpath(completePath, :select=>"id") metric_instance = MetricInstance.find(:first, :conditions=>[

join not giving required result when using or_like codeigniter

纵饮孤独 提交于 2019-12-12 05:35:08
问题 $skills = explode(',', 'good listener,topper,leader'); $teacherID = 7; function search_follower($teacherID, $skills, $limit, $start) { $this->db->limit($limit, $start); $this->db->select('student_skills.user_id'); $this->db->from('student_skills'); $this->db->join('followers', 'followers.student_id = student_skills.user_id', 'FULL JOIN'); foreach ($skills as $skill) { $this->db->like('student_skills.name', $skill); $this->db->or_like('student_skills.name', $skill); } $this->db->where(

rails complex order_by with argument

给你一囗甜甜゛ 提交于 2019-12-12 05:25:10
问题 I have a rails app. I would like to display user profiles ordered by the number of the common tasks they have with the current user. Every task has one assigner and one executor. The number should include both the executed_tasks and assigned_tasks for the same user. So for example if current_user assigned 5 tasks to User4 and User4 assigned 3 tasks to current_user then this number would be 8. My main problem is that I don't know how to use the given user as arg for the count. Should I do in

Rails Multiplying value of afcolumn using ActiveRecord

一曲冷凌霜 提交于 2019-12-12 05:17:39
问题 I want to multiply a value of an specific column considering the user id. Assume I have a table users with user 1 (id 1) and user 2 (id 2), and a table animals which has name and mensal_cost . Ok, then I added two animals for user 1 (id 1) and 1 animal for user 2 (id 2) I want to know how I can using ActiveRecord calculates the mensal_cost income after 3 months increasing the same base value, it means I have to multiply the actual value by 3. I'm trying something like this: Animal.where(user

How to create a new record or update if a particular record based on an attribute other than record id exists, Ruby on Rails?

烈酒焚心 提交于 2019-12-12 05:12:54
问题 I have 2 model classes, employee & company. Only an employee id is generated by an external library. I am trying to update an employee details if his details already exist, else I need to create a new employee details. following is the code for create method: def create if !@emp= Details.find_or_create_by_emp_id(params[:details][:emp_id]) @emp = Details.new(params[:details]) // some logic goes here else @emp.update_attributes(params[:details]) render action: "show" end end But this always

ActiveRecord::Enum - PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer:

对着背影说爱祢 提交于 2019-12-12 05:11:10
问题 I am having a strange error and was hoping someone could point me in the right direction. I have a model called Organizations, and a attribute called department , see the excerpt from the schema below: t.integer "department", default: 0 Inside my model have defined my enums for this attribute, as I am using ActiveRecord::Enum, like below: enum department: [:conferences, :design_teams, :services, :clubs, :events, :communications] But when I query, JobPosting.joins(job: :organization).where

Ordering by number of associations in common even if none (rails)

天大地大妈咪最大 提交于 2019-12-12 05:05:20
问题 I am working on a project which has a very similar quandary to this ask: Ordering by number of associations in common (Rails) The asker of said ask (Neon) writes, BACKGROUND: I have Posts and Users, and both have many Communities. OBJECTIVE: For any given User I'd like to return a collection of Posts, ordered by how many communities the post has in common with the user (posts with more communities in-common being higher up) Unfortunately, the solution only includes posts that have at least