How can I write the following query in Rails?

I need to write a query that only counts individuals that have not attended an event previously. Here are the models class Individual < ActiveRecord::Base has_many :attendances, dependent: :destroy has_many :events, through: :attendances end class At

ActiveRecord validates uniqueness against the subset of records

I have a Contact model that has an email attribute. Contact belongs_to :company and Company has_many :contacts. I would like to add a uniqueness validation on Contact that ensures that the email address is unique within the subset of Contacts that be

Access additional values ​​on has_many via Rails

I am having real trouble accessing the value of an additional parameter called permission on a has_many through. It is probably something simple. My 3 Models are class User < ActiveRecord::Base has_many :players_users has_many :players, through: :pla

Before creating in the active record

I would like to setup a before_create for all of my modules what i have been trying is: module ActiveRecord module UserMonitor require 'securerandom' before_create :attach_uuid def attach_uuid self.uuid = SecureRandom.uuid.gsub("-","")

Join a table and a filtered table in ActiveRecord

Suppose I have a one-to-many relationship between Album and Song classes. I would like to do some complex filtering on the albums and retrieve the corresponding songs. Let's assume a fairly simple way to filter albums filtered_albums = Album.where('r

How to test the recall method of the model independently?

I had a method in a model: class Article < ActiveRecord::Base def do_something end end I also had a unit test for this method: # spec/models/article_spec.rb describe "#do_something" do @article = FactoryGirl.create(:article) it "should w

New on ActiveRecord. Is SQL knowledge important?

I have no experience in SQl databases. I just started using ActiveRecord and I find myself compy pasting the ActiveRecord query statements beucase I don't know difference between when to use .find() .where(), joins etc. There's so many ways to do one

Rails 3 DateTime comparison w / date in an ActiveRecord query

I'm trying to search a model for any dates equal to a specific date while omitting the timestamp. In Rails I could simply execute this as DateTime.to_date == somedate, however I don't think it's quite as easy to formulate in SQL where I wouldn't be a

Rails does not trigger my recall after_commit

Hello I have a peculiar problem. I use an after_commit callback to send notifications, but it seems the callback is not triggered at all. Simplified the situation looks like this: class Message < ActiveRecord::Base after_commit :do_something def do_s

Rails Compares values ​​in ActiveRecord Relations

this is a real hair-puller. I've got a junction table that I need to query for 2 different entries, then update the junction table if the values are not there already. Essentially I have this: master_actors = [#<Actor2role actorId: 13176, dogTag: 459

rails, activateecord, get the current connection specification

I am writing code that will migrate some data from one database to another, over-writing some data in the destination. It uses ActiveRecord, since it's associated with a Rails app using AR already. Since some data will be over-written, I'd like to pr

belongs to: class_name behaving weirdly

I have a Task model and User model. A task can be assigned a user to process it, while a user can be assigned many tasks. The field names are a little off so I am trying to use the class_name options etc, but for whatever reason I'm getting some odd

Best way to randomize (Ruby / Rails 3)

This question already has an answer here: Rails select random record 7 answers I'm currently using: @users = User.order("RANDOM()").limit(6) to generate a list of 6 random users - however, this method takes a 1200ms toll on page load times. Is t

Include a virtual attribute in the respond_with hash

I am trying to include a virtual attribute/method within a respond_to JSON hash. The Model (employee.rb) attr_reader :my_method def my_method return "foobar" end The Controller (employees_controller.rb) respond_to :json def index @employees = Em

Ruby + ActiveRecord: Reading from the database

I've written a Ruby script that parses a tab-delimited text file and writes the output to a sqlite3 database with ActiveRecord. Next, I need to write another Ruby script to display the database's contents as static HTML. The text file that I'm workin

How to create a class dynamically in Java

I don't know if this is even possible. Anyway, here is my problem: I want to create a Class having a database table schema, for example suppose that I have a table like id - unsigned int username - varchar(128) password - varchar(128) and let's assum

ActiveRecord does not set a date / time field

I have a datetime field called time in my MotionRecord model. I try to set it using this command: MotionRecord.create({:time=> "2010-10-15 15:10:24", :chart_id=>1}) Oddly enough this results in the following input: <MotionRecord id: 1,

Active registration with delegate and conditions

Is it possible to use delegate in your Active Record model and use conditions like :if on it? class User < ApplicationRecord delegate :company, :to => :master, :if => :has_master? belongs_to :master, :class_name => "User" def has_mas

Implicit Tables in Ruby on Rails

I'm just learning how to program in Ruby on Rails, and I'm really impressed by how much work the Ruby on Rails framework does for you. I have the following two classes: Object -------- object_id description ObjectGroup -------- group_id description T

Rails / ActiveRecord: Field Normalization

I'm trying to remove the commas from a field in a model. I want the user to type a number, i.e. 10,000 and that number should be stored in the database as 10000. I was hoping that I could do some model-side normalization to remove the comma. I don't