activerecord

rails way user to user messaging, do I need a join table?

Quick question (I think). I have users, and I would like to allow them to send messages to one another. My question is, should I have a user table, a message table, and a users to messages join table, or should I just store to_user_id and from_user_id in the messages table. If the latter, what would the association look like for that? ...

Rails: How do I call `self.save` in my model and have it persist in the database?

I'm trying to keep model logic within my model, but I can't get it to perform modifications on itself and have them persist in the database. In my controller: @article.perform_some_calulcations! In my model: def perform_some_calculations! self.foo.gsub!(/regexp/, 'string') self.save end If I drop debugger statements into my me...

ActiveRecord: How do I add NOLOCK ?

I need to add "WITH NOLOCK" when using ActiveRecord? I know there is a way to do it nHibernate, but couldn't figure this out in ActiveRecord. Thank you for your help. Regards, ...

Rails 3 - select with Include?

I've been looking for how to do a select with include in Rails 3 - I have a nested example: @items = Item.where("fulfilled = ?", true).includes({:order=>[:supplier, :agent]}, :manufacturer) This is a very taxing query to run without selects as it creates 1000s of rows of data by pulling information from all the above big tables. How ca...

How can I populate an activerecord model (and it's associations) from an uploaded file

I have a rails app where I'd like to populate the fields and associations of a model through a file upload. The user is presented with a form with a file upload input element, then after clicking submit, the rails app parses the file and uses it to build a number of other models. A simplified example would look like this (note that for ...

ruby active_record primary key retrieval/custom primary key update

I'm working on a legacy database system where the primary key, for some strange reason, starts with 1 and is not set to AUTO INCREMENT and the next value is always max primary key + 1 (the primary key is still an integer). Furthermore, the primary key names are all custom and set by set_primary_key. Now I want to map ActiveRecord to it ...

Activerecord removes all zeros from the timestamp field.

Hi, I am using Ruby on rails. I am trying to retrieve timestamp column from particular table. Activerecord returns me the date, if timestamp contains all zeros. e.g: If timestamp stored is 2010-09-06 00:00:00:000, it returns me 2010-09-06. But if I have 2010-09-06 00:00:20:000, it returns me in the expected format(i.e: 2010-09-06 00:...

Rails nested resources: Input vs. output format inconsistency

Given the following two models: class Company < ActiveRecord::Base has_many :departments accepts_nested_attributes_for :departments end class Department < ActiveRecord::Base belongs_to :company end I can now create a company and its departments in one go: @company = Company.create! params[:company] In this example params[:co...

How to loop through ActiveRecord values in Rails?

I have the following columns in my table: value1 value2 value3 value4 value5 I want to be able to loop through them like this: <% for i in 1..5 %> <div><%= user."value#{i}"</div> <% end %> Of course this code doesn't work, so how can I get the value from an ActiveRecord object with a string? ...

accepts_nested_attributes_for in combination with formtastic and paperclip

Hi guys I am trying I have a simple one-to-many association. I am trying to update photos that belong to an album through a nested form: The edit form for the photo album: <%= semantic_form_for(@album, :url => user_album_path(@user, @album), :html => {:method => :put} ) do |f| %> <%= f.inputs do %> <%= f.input :title %> <%= f.in...

Stubbing Chained Queries in Rails 3 and Rspec

I'm trying to test a scope I have that is based upon a chain of other scopes. ("public_stream" below). scope :public, where("entries.privacy = 'public'") scope :completed, where("entries.observation <> '' AND entries.application <> ''") scope :without_user, lambda { |user| where("entries.user_id <> ?", user.id) } scope :public_stream, l...

How can I override a method in a ConnectionAdapter class in Rails 3 for use in a rake task?

In order to override the table_exists? method in the Rails PostgreSQL adapter I have tried the following in an initializer file: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do def table_exists?(name) raise 'got here' end end This will raise the the following error: uninitialized constant ActiveRecord::Conne...

Rails 3 Override Destroy without Canceling callbacks, and triggering a ROLLBACK

My desired behavior is that when destroy is called on an instance, that instance will not actually be destroyed, but it will just be marked as having been destroyed. This needs to percolate up for any model associations. In the models that I don't want to actually destroy, but just mark them as deleted I have a deactivated field. From...

Rails3 ActiveRecords displaying column with wrong datatype?

I am not sure how to title this question, its something rather weird. Well, here is the thing, I've created a model called UserProfiles for which the migration file looks like this: class CreateUserProfiles < ActiveRecord::Migration def self.up create_table :user_profiles, :primary_key => :id, :options => "auto_increment = 1" d...

Problem with Active Record Querying

Hi, I am building a little application in Rails and what I am trying to do now is authenticate a user. So I got this method in the controller class: def login if @user = User.authenticate(params[:txt_login], params[:txt_password]) session[:current_user_id] = @user.id redirect_to root_url end end He...

Model traversal in rails: from child to sibling's child

Hi, I have the following model: class Advisor < ActiveRecord::Base belongs_to :course end class Course < ActiveRecord::Base has_many :advisors has_many :sessions has_many :materials, :through=>:sessions end class Session < ActiveRecord::Base belongs_to :course has_many :materials end class Material < ActiveRecord::Base ...

Rails booleans bug ?

I have a rather misterious problem dealing with booleans in Rails, here is how to reproduce: rails new boolean_bug rails generate model User verified:boolean With this you should have an empty project with the User model. 3 Inside boolean_bug/app/models/user.rb ' class User < ActiveRecord::Base before_save :set_false attr_access...

break down a complex search query in Rails 3

I have a controller which has a lot of options being sent to it via a form and I'm wondering how best to separate them out as they are not all being used simultaneously. Ie sometimes no, tags, sometimes no price specified. For prices I have a default price set so I can work around with it always being there, but the tags either need to...

Will Active Record help me concatenate two strings in a database-neutral way?

I'm adding SQLite support to a Rails 2.3 codebase designed for MySQL. Most of the changes so far have been fairly straightforward, but I've now found some examples like the following code: SomeModel.find(:first, :conditions => ["? LIKE CONCAT(some_attribute, '%')", a_variable] ) I can't just use the ANSI concatenation operator beca...

Rails relationship's advice.

I need to model the following relationships and would like some advice on how to properly model it. There is a user, house, apartment, garden, furniture So a user can either have a house or an apartment but not both. Both house and apartment can have furniture but only the house can have garden. So the biggest issue is user has_one ...