Tag: activerecord

How can I use activerecord's query interface and scopes to identify the favorite in a race

I have two models Starter and Race. Starter belongs_to :race Race has_many :starters The attributes for Starter and race follow: Starter attributes: id, race_id, finish_position, odds Race: id, race_date, race_number, field_size I’m trying to accomplish two things: Select the favorite in each race. #for a particular race the Starter with the lowest odds* Select any […]

has_many :through rails

I want to get user1 to accept requests from other users to join user1s post as contributors and be listed out if user1 accepts them Can’t seem to get it to work – Here’s my setup: has_many :through I’m thinking: post model class Post < ActiveRecord::Base #Join table associations has_many :group_requests, class_name: “Group”, foreign_key: “requester_id”, […]

Bug with ActiveRecords

I can’t delete information about tables and columns from ActiveRecord’s cache. I’m using ActiveRecord for Ruby without Rails. require ‘active_record’ ActiveRecord::Base.establish_connection( :adapter => “mysql2”, :database => # :password => # ) class Person < ActiveRecord::Base belongs_to :peoples end enter code here class Persons < ActiveRecord::Base belongs_to :peoples end class People < ActiveRecord::Base has_many :persons end […]

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

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] […]

where should ActiveRecord::Base.transaction be?

I have three models: List, Food, and Quantity. List and Food are associated through Quantity via has_many :through. So each Quantity has three params: food_id, list_id, and amount (an integer). My aim is to create a new Quantity (associated with that list) each time a list is created. I wish to do this using a […]

ActiveRecord ignores table_name_prefix

I’ve got situation where I need to define table_name and table_name_prefix within a model and for some reason table_name overrides table_name_prefix. class ScheduleItem < ActiveRecord::Base self.table_name = “schedule_item” self.table_name_prefix = ‘ACQ_IBM_T.’ end The prefix is completely ignored in the queries. But, when I comment out the table_name part then the prefix is added. Anyone has […]

ActiveRecord/sqlite3 column type lost in table view?

I have the following ActiveRecord testcase that mimics my problem. I have a People table with one attribute being a date. I create a view over that table adding one column which is just that date plus 20 minutes: #!/usr/bin/env ruby %w|pp rubygems active_record irb active_support date|.each {|lib| require lib} ActiveRecord::Base.establish_connection( :adapter => “sqlite3”, :database […]

Rails is not saving an attribute that is changed

This question already has an answer here: New data not persisting to Rails array column on Postgres 3 answers

Find all students not enrolled in a class (rails)

Been trying to figure this one out for a while and I’m really stumped. So I have a danceclass model class Danceclass < ActiveRecord::Base has_many :danceclass_students has_many :students, through: :danceclass_students I Have a student model: class Student < ActiveRecord::Base has_many :danceclass_students has_many :danceclasses, through: :danceclass_students I have my join model: class DanceclassStudent < ActiveRecord::Base belongs_to […]

Rails app error – ActiveRecord::PendingMigrationError Migrations are pending; run 'rake db:migrate RAILS_ENV=development' to resolve this issue

The database is created, tables are created, data are there. But after I restarted the Rails application, I got this error. The app is using MySQL. What’s the issue here? Thank you in advance

Ruby is the best programming language in the world - Ruby on Rails.