Displaying 20 results from an estimated 60000 matches similar to: "How is AR bind supposed to be used?"
2012 Mar 22
1
why doesn't .where() honor polymorphism?
I love the query interface (ActiveMethod::QueryMethods), but one thing
has bugged me: why doesn''t it generate calls for polymorphic
associations? For example:
class MyModel < ActiveRecord::Base
belongs_to :parent, :polymorphic => true
end
I would expect:
MyModel.where(:parent => a)
to be equivalent to:
MyModel.where("my_models.parent_id = ? AND
2010 Jun 11
2
AR after_initialize quandry
I have a case where I am abstracting the creation of models. After a
fair bit of indirection, I eventually get around to making a .new call.
I need to be able to pass params to that .new call.
I know I can''t use def initialize() in an AR model (rather frustrating).
So, I have found some references to after_initialize, but that doesn''t
work either.
I see several threads on
2012 Sep 18
2
Access block scope from AR object
I need to create a sort of transaction around some Active Record objects
that perform many insert/updates.
Example:
class MyObject
def transaction &block
@my_var = value
self.instance_eval block
end
end
MyObject.new.transaction do
Model.create
Model.collection.create
etc......
end
I have a gem that add a method into AR::Base and I need to read
2011 Apr 05
1
Why does AR use base_class for polymorphic relationship "type"
I have a couple models that inherit from a base class that then
inherits from AR.
When they are used in a polymorphic relationship the "type" is always
the base class.
Just curious if anyone has a reason why it doesn''t use the *actual*
class rather then the base_class. I have patched AR to use the actual
class if !base_class.table_exists? but it adds another query each time
2012 Jan 04
1
AR update / create pattern: is there an easier way?
I find myself writing the following frequently:
r = MyActiveRecord.where(:attr1 => cond1, :attr2 => cond2, ...)
r.exists? ? r.first : r.create
... which has the effect of returning an instance of MyActiveRecord if
all the conditions are met, or creating one if it doesn''t. A variant of
this:
c = MyActiveRecord.new(:attr1 => val1, :attr2 => val2, ...)
r =
2009 Dec 15
0
Problems reloading plugin extending AR model
Hi,
I have an AR model called Entity, which is extended in a plugin like
this (this is in a file under lib/ which is required by init.rb of the
plugin):
http://github.com/dedomenon/madb_notifications/blob/master/lib/madb_notifications_lib.rb#L8
Entity.class_eval do
def user_subscribed_to_creation?(user_id)
NotificationSubscription.find(:first, :conditions => [
"source_filter
2011 Feb 08
0
favorite pattern for adding functionality to an AR?
Do you have a preferred programming pattern for adding functionality to
an ActiveRecord? As an example, say I have some gnarly trig functions
for distance and bearing between pairs of latitude and longitude:
=== file: latlng.rb
module LatLng
def haversine_distance(lat1, lng1, lat2, lng2)
...
end
def bearing(lat1, lng1, lat2, lng2)
...
end
=== EOF
... and I want to mix in
2011 Oct 28
3
Is active record 3.1.1 supposed to be threadsafe?
Hi There
Is active record 3.1.1 supposed to be threadsafe?
In other words, should the following (concurrent access to a model)
work?
Thread.new do MyModel.where(some_condition).first end
Thread.new do MyModel.where(some_condition).first end
I ask because upon the answer, it depends which component should handle
synchronization, and in consequence, which component to patch/fix.
A bit of
2012 Jan 11
4
ActiveRecord: ar-each_model
Hi,
today I went into a problem, when I had to iterate over a big result
set. ActiveRecord produces a huge array of model instances, which
consumed 1GB of memory on my machine. A comparable perl DBI script
only used some KB for iterating over the result set.
Then I was suggested to use batch (#find_each) for the problem. But
basically batch does the same thing by splitting the query into
several
2012 Mar 02
0
ConnectionNotEstablished when using connection on AR::Base abstract subclass
I''m using AR outside of Rails and calling establish_connection on an
ActiveRecord::Base abstract_class subclass (to protect my connection
in case anyone else is using AR). It was working just fine with AR
3.0.9, but I recently attempted to upgrade to AR 3.2.2. Now, when I
hit a "nested" query from a has_many, I get a
ActiveRecord::ConnectionNotEstablished. If I call
2010 Sep 08
2
Rails 2 to Rails 3 - How to convert this sql query ?
Hello all,
Can someone help me with this query ?
Here is the query used in rails 2. The idea here is to obtain the top
10 widgets.
Widget.find(:all, :select => ''widget_type_id, COUNT(widget_type_id) AS
widgettypeid'', :group => ''widget_type_id'', :order => ''widgettypeid
DESC'', :limit => 10)
This is deprecated and i want to port it
2010 Apr 21
12
Using AR.connection.execute(sql) -- messing with my string?
Does ActiveRecord.connection.execute(string) perform any modifications
on the given string? If so, what quoting do I need to add?
Here''s why: I''m trying to use execute(string) to load a stored
procedure. I can hand-load the procedure without error (i.e. via
dbconsole), but when I call the function below, it throws the error:
====
ActiveRecord::StatementInvalid: Mysql::Error:
2013 May 28
3
AR's find_by_* limits return results. How to avoid id?
When I use find_by_name method I get LIMIT 1 result.
> SELECT "users".* FROM "users" WHERE "users"."age" = 25 LIMIT 1
But the "age" column is not unique in my table and its value may repeat.
So I expect that an array of records where "age" is 25 will be returned.
But AR limits the query by 1.
How to avoid it? Or is it the default
2012 May 13
2
multiple arguments to select
I read in the Rails 3 Way book that when using select(), you can, in
addition to adding calculated columns (i.g. using sql aggregate
functions), include additional attributes in resulting object by
passing the wild card like this:
Unit.select(:*,"sum(unit_type_id) as
total").group("created_at").having(["created_at > ?", 2.days.ago])
That should give you the new
2012 Jun 07
3
Couldn't reload a module when it's changed. Tried :require_dependency, :watchable_dirs, :unloadable
I''m completely messed with module reloading in development mode. I have
to restart app server each time I edit lib/z/nginx.rb which is included
in app/models/user_case.rb
app/models/user_case.rb
require_dependency ''z/nginx''
class UserCase < ActiveRecord::Base
include Z::Nginx
unloadable
I added to environment.rb
2011 Aug 26
1
Problems with AJAX and bind
I have a list of topics which is loaded as a partial into a div.
Each topic has a destroy action and an id. It works great to add topics
and also to delete, except that I can''t make the loader symbol to stop
running.
I guess do to the fact that the link, the initializer, of the
data-remote action is destroy so it doesn''t know when to run
''ajax:success''.
How
2010 Jan 04
2
to_parm and how it can be used in a helper
is there a way to pass the data returned by the to_parm function in a
helper?
i followed the info provided at
http://www.jroller.com/obie/entry/seo_optimization_of_urls_in and am
able to successfully get the correct urls, now i want to be able to get
the same (id-whatever) text and pass it to a helper function for further
processing.
Any ideas as to how this can be achieved?
--
Posted via
2011 Mar 06
2
Can JRuby be used to "compile" or "encrypt" code?
Coming from PHP, I''m used to seeing things like Zend Encoder that can
take PHP and pseudo-compile it, obscuring the source (useful in the
event you want to sell a product written in said language). I haven''t
found a direct equivalent with Ruby (though if you know of one I''d
like to hear about it), but it occurs to me that JRuby might be used
to do something similar.
2012 Jul 17
1
AR deprecated finders
Hi all,
I had troubles while running tests on Active Record:
bundle exec rake test_sqlite3
/Users/oscardelben/code/rails/activesupport/lib/active_support/core_ext/hash/keys.rb:70:in `block in assert_valid_keys'': Unknown key: extend (ArgumentError)
This is because some models used by tests are using the extend option which now seems deprecated.
Should related tests be
2010 Mar 01
3
has_many models in a form
Hi, guys.
There are four models in the program, A has_many B through C, B has_many
A through C, A has_many D, D belongs_to A. I have a form to create A.
Inspired by Ryan Bates'' railscasts I was able to create A and D in the
same form, and link A to B(created beforehand) through C in the same
form as well. Is it possible to create A and B in the same form?
Thanks in advance.
--
Posted