activerecord

How to get the list of attributes of a activerecord model that can be mass-assigned

I would like to have a list of all attribute names that can be mass assigned. I need this for a custom form builder that will not add input fields by default that cannot be mass assigned. For example if I have a model like : class Post < ActiveRecord::Base attr_protected :account belongs_to :author validates_presence_of :title, ...

Rails RSpec with Multiple Databases

I run a Rails app, and we're in the process of splitting out our signup process to a separate app. The signup app has its own separate database (for CMS and collecting prospects), but it also needs to have access to the main database. This works really well using ActiveRecord::Base.establish_connection. However, I'd like to be able to ...

How do rails association methods work?

How do rails association methods work? Lets consider this example class User < ActiveRecord::Base has_many :articles end class Article < ActiveRecord::Base belongs_to :user end Now I can do something like @user = User.find(:first) @user.articles This fetches me articles belonging to that user. So far so good. Now further I ...

Rails: order using a has_many/belongs_to relationship

Hello, I was wondering if it was possible to use the find method to order the results based on a class's has_many relationship with another class. e.g. # has the columns id, name class Dog < ActiveRecord::Base has_many :dog_tags end # has the columns id, color, dog_id class DogTags < ActiveRecord::Base belongs_to :dog end and I w...

Update attributes unless blank?

I have an existing Project record, and I'm importing a CSV file to update the associated Project attributes. However, often the CSV will contain blank fields and I don't want to overright exisiting attributes if the related CSV field is blank. Something like this: project.update_attributes(:name => row.field('project_name') unless row...

Saving has_many records on create of the belongs_to record

class Bill < ActiveRecord::Base has_many :invoices end class Invoice < ActiveRecord::Base belongs_to :bill end Then in my form I've got several fields for invoices all with the name bill[invoice_names][] so I can access them from params[:bill][:invoice_names]. At the moment I've got a method like this on my Bill Model: #bill.rb ...

Why is it bad to use models as enumerations?

I was just watching a preview of a session at Aloha on Rails called "You're Doing it Wrong". In the short preview, he mentions using ActiveRecord models as enumerations (I assume he means plugins like enumerate_by). This seems like reasonable idea to me, what are the problems? Is it just the overhead required by the additional objects? ...

RoR ActiveRecord find_by_sql Question

I want to preface this question by stating that I am fairly new to Ruby development, however, personally, I've dedicated myself to trying to find the answers myself, as opposed to whimsically asking questions on forums. That being said, this is my first official "post", and I have made every attempt to be as accurate and concise in my po...

Rails ActiveRecord Table Indexes - When should they be used?

I have heard over and over that you should add indexes to any foreign key you will be doing joins on. I have also heard you should have indexes for fields you will do queries on. Does anyone have a fairly exhaustive list or set of guidelines around when and when not to add indexes? There must be a size of table where it is inefficie...

Rails ActiveRecord fetch records with appropriate data types

Hi, I am using Ruby, I want data in array of hash format. I have tried out ActiveRecord's select_al method. But it returns all the data in string format, does not matter what type is its type in data base. All numbers are also returned in string format. Please, give your valuable suggestions. Thank you all for your precious time. T...

Castle ActiveRecord "Could not compile the mapping document: (string)"

Hi I am having getting an exception when trying to initialize ActiveRecord and I cannot figure out what I am missing. I am trying to convince the company I work for to use Castle ActiveRecord and it won't look good if I can't demonstrate how it works. I have work on projects before with Castle ActiveRecord and I had never experience this...

How do I create a default value for attributes in Rails activerecord's model?

Hi all, I want to create a default value for an attribute by defining it in ActiveRecord. By default everytime the record is created, I want to have a default value for attribute :status. I tried to do this: class Task < ActiveRecord::Base def status=(status) status = 'P' write_attribute(:status, status) end end But upon ...

"uninitialized constant ActiveRecord" on migration

Hello, have a problem creating my new table in SqlLite3 I have created this migration using the scaffolding generator: class CreateTimes < ActiveRecord::Migration def self.up create_table :times do |t| t.integer :regsite t.integer :user_id t.timestamp :added t.integer :time t.text :note t.time...

The Authlogic record method. What does this do

I came across this method called record that Ryan bates uses in his authlogic Railscast and can't seem to understand what it does. I have been through the documentation but I can't seem to follow how that helper is useful. def current_user return @current_user if defined?(@current_user) current_user_session && current_user_session....

Validating a Rails model post-save?

I have a model with a couple of accepts_nested_attributes_for. There is a requirement that I have at least one of each of the nested attributes when saving and moving on. However, validation occurs pre-save, so when I'm removing an item and moving on, it let's it through. How can I validate that when I've saved, I have at least one it...

How to determine if a Rails object is marked_for_destruction?

I have some objects which happen to be nested_attributes of something else. When they are marked to be deleted, Rails creates a property "marked_for_destruction". How do I read this var? Sample Yaml dump: --- &id001 !ruby/object:LineItem attributes: name:Pay created_at: 2009-10-12 16:30:51 updated_at: 2009-10-12 16:30:51 stat...

Select, group and sum results from database

Hello I have a database with some fields I'd like to sum. But that's not the big problem, I want to group those fields by the month they were created. ActiveRecord automaticaly created a field named "created_at". So my question; how can I group the result by month, then sum the fields for each month? Updated with code @hours = Hour.al...

ActiveRecord Virtual Attributes treaded as a record attributes

I am running into a problem with to_json not rendering my virtual attributes class Location < ActiveRecord::Base belongs_to :event before_create :generate_oid validates_associated :event attr_accessor :event_oid def event_oid @event_oid = event.oid end end event_oid is not part of the array returned by:...

How to filter by association count?

Let's say I have models that look like this: class Foo < ActiveRecord::Base has_many :bars, :through => :cakes has_many :cakes end class Bar < ActiveRecord::Base has_many :foos, :through => :cakes has_many :cakes end class Cake < ActiveRecord::Base belongs_to :foo belongs_to :bar end How would I get all foo...

Ruby on Rails: Sum table column row values based on other column data.

I have a table with columns 'id', 'resource_id', 'read_time', 'value' where 'value' is a float What I am trying to accomplish is to return a list of records such that the 'value' of each record is the sum of all the records at a specific 'read_time' but having differing 'resource_id' values. I am wondering if there is a clever way (ie ...