If all I have is one model (for example Wiki) and want to save it along with its versions, I could use acts_as_versioned plugin which stores the wikis in "wikis" table and its versions in "wikis_versions" table. This is plain an simple even if I want to moderate the latest version before showing it to the public using a field as status w...
Given an ActiveRecord method or named_scope chain, is there a way to return the SQL that will get executed, without actually running it?
e.g.
Vote.positive.count.sql #=> "SELECT COUNT(*) FROM votes WHERE value > 0"
Is there a built-in way to do this or a plug-in that offers this functionality? If not, any clues to where I can start t...
This question is quite simple but I have run into the problem a few times.
Let's say you do something like:
cars = Vehicle.find_by_num_wheels(4)
cars.each do |c|
puts "#{c.inspect}"
end
This works fine if cars is an array but fails if there is only one car in the database. Obviously I could do something like "if !cars.length.nil?...
I am trying to use a time_select to input a time into a model that will then perform some calculations.
the time_select helper prepares the params that is return so that it can be used in a multi-parameter assignment to an Active Record object.
Something like the following
Parameters: {"commit"=>"Calculate", "authenticity_token"=>"e...
How can I roll my own counter cache for a self-referential many-to-many relationship that uses has_many :through?
I need to track the number of citations and references for each article
I'm using roughly the code from the answer to this question:
class Publication < ActiveRecord::Base
has_many :citations
has_many :cited_publicatio...
How can I write an AR find query to have the results ordered by the number of records in a has_many association?
class User < ActiveRecord::Base
has_many :photos
end
I want to do something like...
User.find(:all, :order => photos.count)
I realize my find is not valid code. Say I have the following data.
User 1, which has 3 phot...
I'm trying to define multiple polymorphic relations (has_many_polymorphs plugin) from a single parent to same children.
Note has many viewers
Note has many editors
Viewers could be either Users or Groups
Editors could be either Users or Groups
Permission is the association model with note_id, viewer_id, viewer_type, editor_id, editor_ty...
Dear All:
Hi! These last two weeks have been my first experience with Castle ActiveRecord, and with the ActiveRecord pattern in general. I'm working on a large system that uses it frequently, and I have been finding some strange SQL transaction issues (such as the one below) as I work on it. I'll give a simplified version of the one ...
I've just been researching the ActiveRecord pattern, and based on this (http://en.wikipedia.org/wiki/Active_record_pattern), it seems Linq 2 Sql more or less implements this, am I wrong? or what would need to be changed for it to conform to the ActiveRecord pattern?
...
Hi,
I have 10 models and 3 of them need some additional custom methods which happen to be:
has_staged_version?
apply_staged_version
discard_staged_version
I want to define these methods once.
I could create a custom subclass of ActiveRecord:Base, define the methods there, and have my 3 models inherit from it. But is there a more "Ru...
There's a great answer for my question over here http://stackoverflow.com/questions/378376/rails-shared-sessions-with-activerecord but it has to do with rails 2.2.2. The method used has been deprecated in 2.3.2.
Does anyone know how to use an external database for session data in rails 2.3.2? The overall goal is session sharing betwee...
I have no idea how I would set up a BerkelyDB database in a Ruby or Rails project.
Does anyone have any experience configuring one, that they could talk about?
Maybe using ActiveRecord or Datamapper?
...
I've looked everywhere for an elegant solution. The essential problem seems to be that ActiveRecord attributes that map to database columns are handled completely differently in ActiveRecord::Base than attr_accessor methods.
I would like to do something like:
model.attribute_names.each do |name|
# do stuff
end
in a way that also i...
Working on a simple example of double sided polymorphic relationships using the has_ many_ polymorphs ActiveRecord plugin. I have two classes, "cats" and "dogs", which can have "friendships" with each other, so three classes in all: "cats", "dogs" and "friendships". A cat can be friends with a dog (yes, it does happen!) and of course als...
Hi,
I have this:
class Bullet < ActiveRecord::Base
include StagedVersionMethods
...
end
And this
module StagedVersionMethods
def initialize
puts self.bullet_id
end
end
When I create an instance of Bullet, the modules initialize method fires, but I get an ActiveRecord error:
...activerecord-2.2.2/lib/active_recor...
I'm interested in learning more about how ActiveRecord was designed and why particular decisions were made along the way that led to the implementation that we have now.
Could anyone provide some examples of good or bad design decisions that were made in ActiveRecord's implementation?
...
Why does the following rails statement
User.find(:all, :joins => [:roles,:roles_users],
:conditions => { :roles => { :name => 'subscriber',
:authorizable_id => self.id,
:authorizable_type => self.class.to_s }})
Translates into this (with 2x the ...
Pretend I have a model, Post which has_many :comments. How can I only display posts that have comments?
I am somewhat comfortable with named_scope but I don't know how I can put Post.comments (or self.comments) in the :conditions hash which expects symbols.
class Post < ActiveRecord::Base
has_many :comments
named_scope :with...
Hi everyone!
I want to show in a view the tasks for a day in concrete (today). these tasks have an estimated time that must sum 6 hours per day.
in the model I have this method:
def self.tasks_for_today
total_time = 0
r = Array.new
all(:conditions => "finished = 0").each do | t |
total_time += t.estimated_time
r << t if ...
My Rails app is starting to need complicated queries. Should I just start using raw SQL queries? What is the trend in the Rails community?
Update:
I do not have written queries right now, I wanted to ask this question before I start. But here is an example of what I want to do:
I have books which have categories. I want to say-
...