activerecord

Can I have thread safe per-request configuration for database connection and table_name in ActiveRecord (or Mongoid)?

喜你入骨 提交于 2020-01-06 03:59:04
问题 Also known as the <<"User has many Databases" question.>> The environment My app is modeled like so: user has_many databases database has_many tables table has_many rows row habtm(+value) columns you get the idea! So instead of modelling a database inside a database, I would like to have: a sqlite3 database that holds the users and many sqlite databases for each user Each user will LCRUD his tables in his databases (similar to phpmyadmin) The problem I would like to have thread safe per

Rails ActiveRecord:: Proper way for validating presence on associations?

邮差的信 提交于 2020-01-06 02:36:07
问题 I have a Rails association between models Project and Queue . A project has_many queues. A queue must have a project, and consequently has a presence validation on project_id Suppose I want to create a new project WITH queues. For example, something like this: project = Project.new(valid: param, options: here) project.queues << Queue.new(other_valid: param, options: here) project.save! The save is going to fail because the queues fail the project_id presence validation. My usual ugly way of

ActiveRecord find categories which contain at least one item

扶醉桌前 提交于 2020-01-06 02:36:05
问题 Support I have two models for items and categories, in a many-to-many relation class Item < ActiveRecord::Base has_and_belongs_to_many :categories class Category < ActiveRecord::Base has_and_belongs_to_many :items Now I want to filter out categories which contain at least one items, what will be the best way to do this? 回答1: please notice, what the other guys answererd is NOT performant! the most performant solution: better to work with a counter_cache and save the items_count in the model!

How to create/update objects in Rails in a many-to-one self-referential model using nested attributes?

谁都会走 提交于 2020-01-05 15:15:29
问题 My Rails 4 app has a topic model that allows many-to-many self-referential relationships. I set up the model based on this fantastic answer on creating a model for many-to-many self joins, and it appears to work if I populate the relationships table manually. Let's use that model for the purposes of the question: user.rb: class User < ActiveRecord::Base # follower_follows "names" the Follow join table for accessing through the follower association has_many :follower_follows, foreign_key:

Creating and enforcing has_one relationship in rails relational database

╄→гoц情女王★ 提交于 2020-01-05 14:25:53
问题 Working on a table of viewed profiles. I kind of have an issue setting it up, is this correct? I am confused about the has_many and has_one relationship. Because this is one table that has a row for each visited relationship, I decided to go with has_one. Does this look correct, also is there a way to enforce the relation in ActiveRecord? model class ViewedProfile < ActiveRecord::Base validates :viewed_profile_id, presence: true validates :profile_id, presence: true has_one :profile_id has

Prevent duplicates from saving in active record, and return the object that was duplicated

谁都会走 提交于 2020-01-05 10:32:25
问题 I'm having a few issues with duplicate records in my database which I believe are being caused by people clicking submit multiple times. I am using Rails 3. Return the duplicated object rather than false I would like to be able to prevent duplicates from being added to my database, but would prefer it if when saving a duplicate, the object that was duplicated was returned, rather than false. The reason being that if someone clicks submit several times, I would like them to be taken to a page

How to find all records with zero number of has_many assocations?

旧巷老猫 提交于 2020-01-05 07:51:27
问题 Let's say you have Question model with following has_many association:(example taken from this plugin) has_many :comment_threads, :class_name => "Comment", :as => :commentable, :dependent => :destroy How would I define a scope or a class method that returns questions that has no associated comments? Basically I want Question.unanswered to return all questions with zero comments. 回答1: I think approach with counter_cache is nicer and faster, but you can create the scope you want like that (you

update fail - undefined method `update_attributes' for #<ActiveRecord::Relation:

扶醉桌前 提交于 2020-01-05 07:13:25
问题 This follows on from my previous request which I answered. But my next issue which I cannot understand is why when my parameters report one record do I get a message that googling/SO searching suggest I need to use an update_all. has_many/belongs_to build association - why is insert statement for parameter blank? My update controller method is as follows: def update @role = Role.find(params[:id]) logger.debug "@role: id[#{params[:id]}], #{@role}, #{@role.inspect}, #{@role.to_s}" @permission =

class_name foreign_key in Rails model

拟墨画扇 提交于 2020-01-05 05:58:25
问题 I recently come across this code. User has many Answer. What is the purpose of the :class_name and :foreign_key ? class Answer < ApplicationRecord belongs_to :user, :class_name => 'Question", :foreign_key => 'question_id' end 回答1: The naming here is kind of strange, but the purpose of :class_name is to allow you to use a class that is different from the one Rails expects. When you have a belongs_to :user on a model, Rails would expect that to point to a parent class called User . In your

How do I sort a parent class based on an attribute of the child class?

隐身守侯 提交于 2020-01-05 04:51:26
问题 I have what seemed to me a simple requirement: I want to sort a parent class based on an attribute of its child class. class owner has_many :tasks end class task belongs_to :owner end Now I want to sort the owners based on the due date on their tasks, so that the owner that has the task with the "closest" due date is first, etc I can do this in Ruby with array sorting, but my record sets are huge and i need to do this with AR / DBMS.. UPDATE: As I am pondering this question part of the