activerecord

How can I extend this Ruby ActiveRecord model?

I'm creating this little show scheduler thing. I have the table shows, with title:string and description:text. I want to add days and times to the show (each show can have multiple days, and each day has their OWN times). How might I go about doing this? I was thinking of making a times table. With the columns (show_id, day, and time)....

How to Return map of parent_id to children_ids in ActiveRecord/SQL?

Say I have this: class Post < ActiveRecord::Base has_many :relationships, :as => :parent has_many :images, :through => :relationships end class Relationship belongs_to :parent, :polymorphic => true belongs_to :child, :polymorphic => true end class Image < ActiveRecord::Base has_many :relationships, :as => :child has_many :...

Can I have thread safe per-request configuration for database connection and table_name in ActiveRecord (or Mongoid)?

Also known as the <<"User has many Databases" question.>> The environment My app is modeled like so: user has_many databases database has_many tables table has_many rows row habtm(+value) columns you get the idea! So instead of modelling a database inside a database, I would like to have: a sqlite3 database that h...

Active record query with includes does not include...

I have got this 'query' @product_collections = ProductCollection. #includes(:products). #does not do anything joins(:products). #this at least gives me the products (in articles table) group("tags.id"). where("articles.category_id = ?", @category.id). where("articles.available = ?", true). ...

Need help optimizing some Rails 2.3 ActiveRecord code

Hi everybody! (Hi Dr. Nick!) I'm trying to tighten things up for our app admin, and in a few places we have some pretty skeezy code. For example, we have Markets, which contain Deals. In several places, we do something like this: @markets = Market.find(:all, :select => ['name, id']) @deals = Deal.find(:all, :select => ['subject, disc...

How to perform 'move field' refactoring on active-record models

This is a fairly common refactoring, Martin Fowler calls it 'move field'. Given 3 models: class Person < ActiveRecord::Base has_one :contact_details has_one :address end class ContactDetails < ActiveRecord::Base end class Address < ActiveRecord::Base end how do I refactor, including migration, the has_one address from Person to ...

ActiveRecord touch(:column) always also updates :updated_at

I've got pretty much the same problem as this guy: http://www.ruby-forum.com/topic/197440. I'm trying to touch a column (:touched_at) without having it auto-update :updated_at, but watching the SQL queries, it always updates both to the current time. I thought it might be something to do with the particular model I was using it on, so ...

after_add callback for has_one

This seems to be an inconsistency between has_many and has_one. The has_many association allows you to specify an after_add callback that is called after an object has been added to the collection. class Person has_many :parents, :after_add => { puts "Added new parent" } # allowed has_one :car, :after_add => { puts "Added car" } #...

Group by multiple columns in ActiveRecord

Using Rails 3.0, i'm trying to do a count on number of times a each combination of column1 and column2 occur. IE Column A has values A-Z and Column B has values 1-5, i want a count of A1, A2, etc. Is there a way to either group by multiple columns or join the two columns and group of the result? In reading the documentation, it wasn...

Rails: any way to refactor this ActiveRecord code?

I have a piece of code that checks that a survey response picked by user to a survey question is in fact one of valid choices: Question.find_by_id(question_id).question_choices.all(:select => 'id').map {|x| x.id}.include?(user_choice_id) Is there an easier way? Thanks! ...

Self referential associations (Ruby on Rails)

I want to create a system for users to comment on posts where comments can also have replies. Since I can't do a self-referential HABTM relationship, I did some research and saw that I should be going about it in this manner: Post has_many :comments end Comment belongs_to :user belongs_to :post has_many :replies, :class_name =...

Finding object in array based on attribute

My app has the following models: user and watch_list. User has attributes id, name and WatchList has attributes user_id, friend_id. class WatchList < ActiveRecord::Base belongs_to :user has_many :friends, :class_name => "User", :foreign_key => "friend_id" end class User < ActiveRecord::Base has_one :watch_list has_many :friend...

Overriding the assignment method for a has_one

I have a has_one association that is reflective on the objects of another association. I have Project, which has many ProjectUsers, which tie Projects and Users. One of those ProjectUsers is authoritative. The issue is that both the has_one and the has_many use the same foreign key on project_users. Here's the base idea of the models...

Rails 3 LIKE query raises exception when using a double colon and a dot

Hi, In rails 3.0.0, the following query works fine: Author.where("name LIKE :input",{:input => "#{params[:q]}%"}).includes(:books).order('created_at') However, when I input as search string (so containing a double colon followed by a dot): aa:.bb I get the following exception: ActiveRecord::StatementInvalid: SQLite3::SQLExc...

Why can't ActiveRecord assign object to association attribute?

I've spent half a working day trying to track this down in AR. Given a model setup like: class Publication < ActiveRecord::Base has_many :subscriptions end class Subscription < ActiveRecord::Base belongs_to :publication belongs_to :user end In controller, @new_subscription = publication.subscriptions.create( user: @current_u...

CodeIgniter ActiveRecord problems

Hi. I'm trying to build my first app on CodeIgniter. This is also my first time trying to stick to OOP and MVC as much as possible. It's been going ok so far but now that I'm trying to write my first model I'm having some troubles. Here's the error I'm getting: A Database Error Occurred Error Number: 1064 You have an error ...

How to write this statement using PHP & CodeIgniter

Hi. I'm trying to figure out how to write a statement in my first CI app (and only second PHP app ever) and I'm stuck. I have a junction table that holds book_id and user_id from their respective tables. I want to find out who has read a book and echo that back out, but I need the formatting to make sense. Here is the query: $readQuer...

how to modify complex find_by_sql query w/ union into rails 3

here's the current query: @feed = RatedActivity.find_by_sql(["(select *, null as queue_id, 3 as model_table_type from rated_activities where user_id in (?)) " + "UNION (select *, null as queue_id, null as rating, 2 as model_table_type from watched_activities where user_id in (?)) " + "UNION (select *, null as rating, 1 as model_tabl...

how to autobuild an associated polymorphic activerecord object in rails 3

class ItemSource < ActiveRecord::Base belongs_to :product, :polymorphic => true end class RandomProduct < ActiveRecord::Base has_one :item_source, :as => :product, :autosave => true, :dependent => :destroy end What I'd like to do is is call: a = RandomProduct.find(1) a.item_source and if item_source doesn't already exist (= nil...

Is there any way to disable Active Record in runtime (or just degradate due to DB down)

Hi! In my Rails 2.3 app there is a possible situation when database is down, but app still should work. I.e. I need to disable part of my application when database is down. E.g., have some method like is_database_up? that will return true if database is up. For now, if database crushes, whole app will crush too. Is there any way to hand...