activerecord

Accessing two sides of a user-user relationship in rails

Basically, I have a users model in my rails app, and a fanship model, to facilitate the ability for users to become 'fans' of each other. In my user model, I have: has_many :fanships has_many :fanofs, :through => :fanships In my fanship model, I have: belongs_to :user belongs_to :fanof, :class_name => "User", :foreign_key => "fanof_...

cant use Activerecord find method with associations.

here are my models: #game class Game < ActiveRecord::Base #relationships with the teams for a given game belongs_to :team_1,:class_name=>"Team",:foreign_key=>"team_1_id" belongs_to :team_2,:class_name=>"Team",:foreign_key=>"team_2_id" def self.find_games(name) items = Game.find(:all,:include=>[:team_1,:team_2] , :conditions ...

rails conditional find based on datetime with timezone

I have a rails (2.3.4) application and want to send users a daily update via mail. To make things more comfortable, users can choose the time when they want their daily mail. My question: how should I store and more importantly lookup this time so as users' time zone, in particular daylight savings are respected? Currently, we save a 'p...

Trying to convert existing production database table columns from enum to VARCHAR (Rails)

Hi everyone, I have a problem that needs me to convert my existing live production (I've duplicated the schema on my local development box, don't worry :)) table column types from enums to a string. Background: Basically, a previous developer left my codebase in absolute shit, migration versions are extremely out of date, and apparen...

What exactly is Arel in Rails 3.0?

I understand that it is a replacement for ActiveRecord and that it uses objects instead of queries. But... why is this better? will objects/queries be "easier" to create? will it lead to more efficient SQL queries? will it be compatible with all major DBs? - I assume it will. will it be easier/harder to use with stored procedures...

How to add a new entry to a multiple has_many association?

I am not sure am I doing these correct. I have 3 models, Account, User, and Event. Account contains a group of Users. Each User have its own username and password for login, but they can access the same Account data under the same Account. Events is create by a User, which other Users in the same Account can also read or edit it. I c...

Codeigniter Active record help

Hello, I am trying to increment a INT column by 1 if a certain field is not null on an update request, currently I have this update too columns, public function updateCronDetails($transaction_reference, $flag, $log) { $data = array ( 'flag' => $flag, 'log' => "$log" ); $this->db->where('transaction_refere...

Rails find all with association

I have what I think is a very simple problem (famous last words)... I have a Category model that has_and_belongs_to_many Events. I want to construct a simple and efficient query that finds all categories that have 1 or more events. (using Rails 3) I'm sure I'm having a dumb moment here - any help appreciated :) ...

Codeigniter: Select from multiple tables

How can I select rows from two or more tables? I'm setting default fields for a form, and I need values from two tables... My current code reads: $this->CI->db->select('*'); $this->CI->db->from('user_profiles'); $this->CI->db->where('user_id' , $id); $user = $this->CI->db->get(); $user = $user->row_array(); $th...

Codeigniter active record sql error

I am using the get_where() function in codeigniter, and I am getting mysql errors, dependent on what I set the limit and offset too, for example this code, $this->db->get_where('em_user', $whereArr, 30, 0)->num_rows() returns a mysql error that looks like this, Error Number: 1064 You have an error in your SQL syntax...

Ruby Rails :has_many, autosetting a column

Given the following Ruby-on-Rails code (1.8.6, 2.3.5): class MyClass < ActiveRecord::Base has_many :modifiers, :conditions => ["affects_class = ?", self.name], :foreign_key => :affects_id end What I'm trying to do is to automatically set the affects_class column to 'MyClass'. In other words: myInstance = MyClass.find(:firs...

Using ActiveRecord caching library in Heroku

Hi all, I'm studying how to use caching in Heroku for my Rails app. HTTP cache powered by Varnish is superb and I'll use it in all pages without user info but I also want to use a kind of ActiveRecord caching with Memcached using "high livel" plugins such as cache_fu or cache-money...but it seems that Heroku supports only the memcached g...

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.estab...

Recalculate Counter Cache of 120k Records [Rails / ActiveRecord]

The following situation: I have a poi model, which has many pictures (1:n). I want to recalculate the counter_cache column, because the values are inconsistent. I've tried to iterate within ruby over each record, but this takes much too long and quits sometimes with some "segmentation fault" bugs. So i wonder, if its possible to do th...

how to add a entry to tables with relationships?

I have 2 models, Users & Accounts. They are in one-to-many relationship, i.e. each accounts have many users. Accounts company_id company_name company_website Users user_id user_name password company_id email How can I add these entries to database using ActiveRecord? Supposed I don't is the company existed i...

read/write_attribure on associations

read/write_attribute is a great way to enhance default accessors generated by ActiveRecord. Like this for example: def price read_attribute(:price) or "This item is priceless and you are by the way #{User.current.login}" end The same however does not seem to be working with associations. Demonstration: class Product < ActiveRecord:...

Why can't I set boolean columns with update?

I'm making a user administration page. For the system I'm creating, users need to be approved. Sometimes, there will be many users to approve, so I'd like to make that easy. I'm storing this as a boolean column called approved. I remembered the Edit Multiple Individually Railscast and thought it would be a great fit. However, I'm ru...

Removing "duplicate objects"

Let's say I have an array of objects from the same class, with two attributes of concern here: name and created_at. How do I find objects with the same name (considered dups) in the array, and then delete the duplicate record in the database. The object with the most-recent created_at date, however, is the one that must be deleted. ...

How Do I Search Between a Date Range, Using the ActiveRecord Model?

I am new to both Ruby and ActiveRecord. I currently have a need to modify and existing piece of code to add a date range in the select. The current piece goes like this: ReportsThirdparty.find(:all, :conditions => {:site_id=>site_id, :campaign_id=>campaign_id, :size_id=>size_id}) Now, I need to add a range, but I am not sure how to d...

MYSQL multiple insert in codeigniter

Hi everyone, I know about the possibility of making a multiple insert in mySQL by doing something like this: foreach ($array as $manuf) { $sql[] = '("'.mysql_real_escape_string($manuf['name']).'", "'.$manuf['lang'].'", "'.$mId.'")'; } $this->db->query('INSERT INTO manufacturers (name, lang ,mid) VALUES ' . impl...