In two tables mapped to ActiveRecord with unknown number of identical columns, e.g.:
Table A Table B
--------- ---------
id id
name name
age email
email is_member
How can I (elegantly) copy all identical attributes from a record of Table A to a record of Table B, except the id attri...
What's the standard/best option out there for this kind of version control? The main thing I'm looking for is to track what user edited a record.
I've seen these so far and am wondering what your take is:
PaperTrail
ActsAsAudited
VestalVersions
...
Hi folks,
I am trying to understand the Active Record Callbacks, but they do not work, like I want to.
e.g.
Model
Checklist<ActiveRecord...
attr_accessible :item1, :item2, :done # they are all boolean
before_save :check_done
private
def check_done
if item1 && item2
write_attribute :done, true
else
write_attribute :done, ...
Hi, I am working on a Rails 3 app, and I need to return back a list of players where the sum of points_received per player, is greater than a specified amount.
I have tried this:
Answer.sum(:points_received, :group => 'player_id').having("points_received > 5")
but of course this gives me an error: NoMethodError: undefined method `ha...
i have the following 2 classes.
class Customer < ActiveRecord::Base
set_table_name "customer"
set_primary_key "customerId"
has_many :new_orders, :foreign_key => "customerid", :primary_key => "customerId", :class_name => "NewOrder"
end
class NewOrder < ActiveRecord::Base
set_table_name "viewNewOrders"
set_primary_key "orderid"
bel...
Hi guys,
What is the best way to store array of of primitive types using Rails activerecord?
For example I've got article model, which has images property. Images property is array of image urls.
I don't wont use separate table to store this array.
Regards,
Alexey Zakharov
...
Is is possible to pass options to a find_or_create method? I'd like to include a couple associations it the record is there.
I though something like this would work but it's not including them.
Event.find_or_create_by_asset_id(asset_id, :include => [:tags, :address])
...
Hi,
I've been breaking the DRY principle a bit in a project built with Paperclip for media storage. I've added assets to certain models, such as AlbumPhotoFile, AlbumSoundFile and so on, using the following code :
# Asset is a regular < ActiveRecord::Base class
class AlbumPhotoFile < Asset
has_attached_file :pic, :url => "foo", :pat...
Hi,
I have a model:
class Shirt < ActiveRecord::Base
named_scope :red, :conditions => { :color => 'red' }
named_scope :blue, :conditions => { :color => 'blue' }
named_scope :xl, :conditions => { :size => 'xl' }
end
I forgot, how to easy add named scope to existing anonymous scope:
scope = Shirt.scoped({})
#and how to add ie...
Using Rails 2.3.8 I've experienced the following problem.
Eager loading works fine When I have a controller like this:
def alternativeview
@books = Book.all :include => :author
respond_to do |format|
format.html { render :action => "index" }
end
end
However, if I want to render this action with a different html....
This one catches me out a lot.
class Repository < ActiveRecord::Base
has_one :database, :storage
before_create :provision_database, :provision_storage
def provision_database
self.database = Database.new # works
end
def provision_storage
storage = Storage.new # doesn't work
end
end
class Database < ActiveRecord::Ba...
Hi,
I have two models:
class Manufacturer < ActiveRecord::Base
has_many :models
end
class Model < ActiveRecord::Base
belongs_to :manufacturer
belongs_to :model
end
What I want to be able to do is find all manufacturers whose models belong to a given category:
manufacturers = Model.find(:all, :conditions=>["vehicle_category_id...
Given the following:
class User < AR::B
has_many :permissions
has_many :projects, :through => :permissions
end
class Project < AR::B
has_many :permissions
has_many :users, :through => :permissions
end
class Role < AR::B
has_many :permissions
end
class Permission < AR::B
belongs_to :user
belongs_to :project
...
I have the following line of Rails 3
In the project.rb model this works great:
permissions.find_by_project_id(1).role.name
However in a projects_helper it errors "undefined method `role' for nil:NilClass":
if Permission.find_by_project_id(project_id).role.name.nil?
.
.
Why is that?
What I really want is:
current_user.permission...
I'm building a command line application using ActiveRecord 3.0 (without rails). How do I clear the query cache that ActiveRecord maintains?
...
I have three models
class Collection < ActiveRecord::Base
has_many :presentations
has_many :galleries, :through => :presentations
end
class Gallery < ActiveRecord::Base
has_many :presentations
has_many :collections, :through => :presentations
end
class Presentation < ActiveRecord::Base
belongs_to :collection
belongs_to...
I have something like this:
class User < ActiveRecord::Base
has_one :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
user = User.new
user.profile.something #=> ERROR
What is a proper way to set a default profile object in this case? I have tried this:
class User < ActiveRecord::Base
default_scope :include...
Hello, in my project.rb model, I'm trying to create a scope with a dynamic variable:
scope :instanceprojects, lambda {
where("projects.instance_id = ?", current_user.instance_id)
}
I get the following error: "undefined local variable or method `current_user' for #"
Where in the controller I can access current_user.instance_id.....
I want to destroy all objects that have parent_type == 'Profile' or child_type == 'Profile', like this:
Relationship.destroy_all(:parent_type => "Profile")
Relationship.destroy_all(:child_type => "Profile")
How do I combine that into one method and one sql call?
Went with something like this:
class Group < ActiveRecord::Base
has_m...
I'm pretty new to optimizing my queries, I have an N+1 query and it seems it needs a counter, but I'm not really sure how to proceed:
...
SQL (0.5ms) SELECT COUNT(*) AS count_id FROM (SELECT 1 FROM `photos` WHERE (`photos`.attachable_id = 4864 AND `photos`.attachable_type = 'Recipe')) AS subquery
SQL (2.1ms) SELECT COUNT(*) AS count_i...