activerecord

Problem with has_many :through and fields_for.

Hi There, I have a pretty basic association: # user.rb class User < ActiveRecord::Base has_many :services, :through => :subscriptions has_many :subscriptions, :accessible => true accepts_nested_attributes_for :subscriptions end # service.rb class Service < ActiveRecord::Base has_many :users, :through => :subscriptions has_ma...

Higher level database layer for Android?

Are there any good database abstraction layers/object relational mappers/ActiveRecord implementations/whatever they are called for Android? I'm aware that db4o is officially supported, but it has quite a large footprint and I'd rather use a more conventional database (SQLite). ...

Rails + C# - Reusing Models

I'm working on a project that has a website in Rails and a C# GUI that use the same database and data models. I'd like to share the (active)models between the two parts. Any ideas on how this is possible? ...

Active Record: Get two random objects?

What's the simplest way to get an array with three objects (Card), one of which I already have? The other two should be randomly selected from the database. My current approach looks like this: [ @deck.cards[rand(@deck.cards.size)], @deck.cards[rand(@deck.cards.size)], @mycard ].sort_by {rand} The problem I have right now is t...

ActiveRecord nested SELECT -- can I do it without manual SQL?

I have a table with (among other things) a name and a rank. I'd like to return the set of all unique names, but for each name returned, I'd like to pick the row with the highest rank. This is simple with two nested SELECT statements: SELECT * FROM (SELECT * FROM foo ORDER BY rank DESC) AS ordered GROUP BY name MySQL takes the first ...

Passing @object into a rails partial render

I have a partial: 'profiles/_show.html.erb' that contains code like <%= @profile.fullname %> I'm trying to render the partial but I'm not sure how to pass the @profile. I tried using local but apparently it sets 'profile' on my partial instead of '@profile'. <%= render :partial => 'profiles/show', :locals => {:profile => @app.profi...

Ruby on Rails. How to show record only once?

Hello. I want to know how to show a record only exactly after it was created. So i want to show it only once. Just see. I have model called Claim. User creates new Claim (user is anonymous). I want to show this Claim only ofter user creates it and never again. How can I do it? I think that I can use flash hash, but i think it is not e...

ruby on rails undefined method(s) for active record ..

(in /Users/sayedgamal/apps/test) /Users/sayedgamal/apps/test/config/boot.rb:20:Warning: Gem::SourceIndex#search support for String patterns is deprecated == CreatePeople: migrating ==================================================== -- create_table(:people) rake aborted! undefined method `string' for #<ActiveRecord::ConnectionAdapters::...

Chained has_many through: users -> roles -> lists. Finding lists that a user is assigned to (via roles).

It must be a very common problem. I have a chained many-to-many relationship like this: User n<==>n Role n<==>n List ActiveRecord models: class User # linking to roles has_many :role_assignments has_many :roles, :through => :role_assignments end class Role # linking back to users has_many :role_assignments has_many :user...

Rails ActiveRecord - eager loading (sort of) when using 'build' from has_many

I have a situation where children are built but not saved, and are then being used in the view with references to the parent. This leads to extensive use of rails record caching. I'd like to have the parent 'eager loaded' with the unsaved children records. class Parent < ActiveRecord::Base has_many :children def make_children lo...

ActiveRecord hating a particular column? (Bizarre)

Hey guys, I've been working on a web app lately, and Activerecord has started creeping me out- for the most part it's awesome, but it has taken to treating a particular column like a leper. Initially I created a student model from a scaffold. The model holds various information like name, email, entry quarter, etc. And that all works b...

Rails & MSSQL 2008 - Will We Hit Barriers?

The company I am working for is looking to switch platforms from ColdFusion 8 / Windows to Ruby on Rails / Linux. Our database solution will remain as MSSQL 2008 on Windows. I will likely follow up with a series of questions relating to this migration separately, but for now I have a MSSQL & Rails specific question. Back in 2006 when I ...

ActiveRecord :select is messing with my data types

So I have this very simple snippet. Topic.all(:select => 'count(*) as cnt')[0].cnt # behaves the same on all models "500" # What, a string? It seems that for some reason ActiveRecord is coercing the count to a string. In fact, I notice it coerces everything in the select list missing from the original object to a string. Why does...

ActiveRecord #find getting wrong class of object

I have two models: CreditCard and BlacklistItem::CreditCard. If I search for a BlacklistItem::CreditCard first, I get the expected behaviour: >> BlacklistItem::CreditCard.find(:all).first => #<BlacklistItem::CreditCard id: 5, *snip* > >> If I search for a CreditCard first, when I go to look for BlacklistItem::CreditCard items later ...

What is the best way to do scoped finds based on access control rules in Rails?

Hi I need to find an elegant solution to scoped finds based on access control rules. Essentially I have the following setup: Users Customers AccessControl - Defines which user has access to another users data Users need to be able to access not just their own customers but also shared customers of other users. Obviously something lik...

Self-referential association in Rails 2.3

I have model called test, and test can have many tests, and should be able to have a reference to it's parent test if it exists. EG test <-- parent doesn't exist test test test test test test test <-- parent doesn't exist i've seen a couple of possible solutions with examples before 2.3, but how models handle references seems t...

I need help with a Rails ActiveRecord Join - I know how to do it in SQL

I have 2 tables I need to join but the user_id column value is not the same in both tables. So I want to do something like this: In my controller 4 will be substituted with current_user.id select * from sites join pickups on sites.id = pickups.site_id where sites.user_id = '4' But using an ActiveRecord Find. Here are my association...

How can I have two columns in one table point to the same column in another with ActiveRecord?

I run the risk of palm-to-forehead here, but I can't quite figure out how to do this with Rails' ActiveRecord sugar. I have a tickets table that has two columns (submitter_id and assignee_id) that should each reference a different user from the users table (specifically the id column in the users table). I'd like to be able to do things...

Putting a condition on a many-to-many query with ActiveRecord

I've got the following relationships in my model: class Show < ActiveRecord::Base has_many :service_shows has_many :services, :through => :service_shows end class Service < ActiveRecord::Base has_many :service_shows has_many :shows, :through => :service_shows end class ServiceShow < ActiveRecord::Base belongs_to :show belo...

Rails fake deletion of a row

I'm looking for a way to faux-delete rows from a table using Rails. The rows shouldn't show up in any normal .find() search except for when I want to show recently deleted items. I'm using an Activities table right now for management of such tasks, which stores the class, id and method performed on rows recently. Is there a way to disab...