activerecord

ActiveRecord doesn't set a datetime 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, time: nil, chart_id: 1> I'm not sure what I am doing wrong. Edit: This is my model. class Moti...

"WARNING: Can't mass-assign protected attributes"

I have used RESTful techniques to generate a model (in fact, I am using Devise gem, which does that for me), and I have added new fields called first_name and last_name to the model. Migration went fine. I added attr_accessor :first_name, :last_name to the model and expected it would just work. But when I try to mass-assign new instances...

Rails and created_at

Hi all! I do: Order.find(:all, :conditions => "created_at>='#{DateTime.now.year}-#{month}-1' and created_at<='#{Date.new(DateTime.now.year, month.to_i, -1)}'") That is working fine except that everything created on the last day of the month is not included. Created_at might contain: "2010-09-30 18:34:09". That is NOT less than or equ...

Proxy Objects with ActiveRecord models - method_missing not working sometimes

Hello all! I've been using a model of my application as a proxy to other objects that define behavior. class Box < ActiveRecord::Base belongs_to :box_behavior, :polymorphic => true, :validate => true, :foreign_key => 'box_behavior_id', :dependent => :destroy [...] def initialize(opts = {}) super(opts) self.box_behavi...

Rails destroy all but newest n records

How do I destroy all but the newest n records using Rails' ActiveRecord? I can get the newest n records using order and limit but how do I destroy the inverse? ...

Is there a clean Rails way to set attributes when creating join models?

I'm using Rails 3 and have a rich association between Projects and Users. The join model (UsersProject) has an additional boolean attribute, administrator, which flags the User as an admin of Project: Sure I'm missing something obvious here, but is there a clean way to set the administrator attribute on the join model (UsersProject) whe...

What am I doing wrong with this query?

Goal: to get a list of N random questions that have been answered correctly the least amount of times, per user. following sql will provide me a list of having a count of correctly answered questions for user_id 4. select q.id, temp.Count from questions as q left join (select q.id, count(a.id) as count from questions as q left join att...

Newbie rails database relation question

I have two models Station and Broadcast. Broadcast belongs_to Station and has a station_id column. I don't know how to make new method in BroadcastController to expect the station_id value and how to create a new Broadcast with right station_id in it. ...

How can I use active record to link two tables with different "types"

I'm trying to create a database with two models a user and a an mp3 file. I want the user to have two relations on the mp3. The following are the use cases I want to tackle. User has created many mp3's many mp3's have been created by a user User own many mp3's (He could have bought and mp3 from a store) many mp3's have been bought by ...

Confusion over the tables for has_one and has_many

In the Rails ActiveRecord Associations guide, I'm confused over why the tables for has_one and has_many are identical: Example tables for has_many: customers(id,name) orders(id,customer_id,order_date) Example tables for has_one: these tables will, at the database level, also allow a supplier to have many accounts, but we just want on...

Rails 3 ActiveRecord Questions

I have a few queries for you Rails 3 gurus out there. How can you accomplish the following? The following pseudocode is currently invalid. Thanks all. @items = (@itemsA + @itemsB).order("name ASC") @item = Item.where("type = ?" and "condition = ?", "book", "new") @commenteditems = Item.find_all_by_type_and_condition("book", "new").in...

ActiveRecord has_many relation prevent orphaned newly created object

Suppose I have an Comment model that belongs_to a Post model. I want to make it so that creation of a new Comment instance (whether by new, create, find_or_create_by_x, etc.) will fail (preferably raise an exception) unless the Post is set immediately (either by passing it in as a parameter or by always referencing the post when creatin...

Finding friends with the same saved show in ActiveRecord (associations/join problem)

My design has Users with Friendships to other Users, and they can all save Shows to a list of SavedShows. Now for an individual show page, I'd like to get a list of the friends for a particular registered user that have the same show saved, but I can't seem to figure out the right query. I thought it would be something like following (3...

Optimize ActiveRecord initialization time (on application start)

For every application_start, I call ActiveRecordStarter.Initialize (actually it's part of the ActiveRecordIntegration facitliy). This seems to be very heavy, takeing around 3.5 seconds to initialize my 16 objects. some background: 1. got 16 AR objects for now, almost never changed (maybe once a week). 2. most of the changes are not AR r...

ActiveRecord variable apart from column fields

I have a table with a user_id field. In find, I created a join on user table to retreive the username as: @question = Question.find(params[:id], :select=>"questions.*, users.username as username",:joins=>" inner join users on users.id = questions.user_id"); I created an instance variable in Question class with name username. But I...

Rails 3 ActiveRecord validation based on user permissions

I'm shifting code from an application built in a non-standard custom PHP framework into Ruby on Rails (version 3). In the PHP version all the controllers are really fat, with thin models, which I've always disagreed with, so I'm enjoying the way Rails does validation at the model level, which is probably 90% of what's happening in these...

Rails - Make a one-off query to a different database and table.

Hi, I have a rails app with a wordpress blog sellotaped on the side (totally separately at /blog). The client want's to the latest blog post on the main homepage of the rails app, so I need to do a one-off mysql query to the word-press database. How would I go about doing this in the rails app. The word-press is completely sperate from...

ActiveRecord/MySQL query to return grouped set of objects

I have a model called a Statement that belongs to a Member. Given an array of members, I want to create a query that will return the most recent statement for each of those members (preferably in one nice clean query). I thought I might be able to achieve this using group and order - something like: # @members is already in an array @...

Problem using Resque, Rails 3 and Active-Recored

I have this Class which response to perform to be run by "Resque", I have an error at this line recipient.response = response.body wich is undefined method response=' for #<Hash:0x00000003969da0> I think that because the worker and ActiveRecord can't work together. P.S I already loaded my environment and this class placed in lib directo...

Rails 3 ActiveRecord associated collections custom methods

If I have an Auction record, which has many Bids associated with it, out of the box I can do things like: highest_bid = auction.bids.last(:all, :order => :amount) But if I want to make this clearer (since it's used in multiple areas in the code), where would I define the method: highest_bid = auction.bids.highest_bid Is this actual...