Displaying 20 results from an estimated 600 matches similar to: "Callbacks on has_many relation"
2010 Jul 12
1
How to turn :before_add exceptions into validation errors
As per http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
Possible callbacks are: before_add, after_add, before_remove and
after_remove.
Should any of the before_add callbacks throw an exception, the
object does not get added to the collection. Same with the
before_remove callbacks; if an exception is thrown the object doesnât
get removed.
However, when the add
2007 Sep 07
5
enforce mutual exclusivity
Hi,
First post, please be gentle! I couldn''t find any mention of this
already
I''m using ActiveRecord and I want to enforce mutual exclusivity on a
has_and_belongs_to_many.
A concrete example: I have a User which can have one or more Roles
(student, tutor, headmaster, administrator etc). However if a user is
a
student they cannot hold any other role. I was hoping to find
2007 Jul 21
3
unable to get "has_many :after_add" to work - need help
Can someone tell me what I''m doing wrong? I''ve spent way too much
time on this
--------------------------------------------------------------------------------------
class Picture < ActiveRecord::Base
has_many :ratings, :after_add => :increment_rating, :after_remove
=> :decrement_rating
def increment_members(rating)
self.rating += rating.amount
save!
2006 Jan 31
2
How can I overwrite the parent.children.push(child) Method?
Hi all
I want to overwrite the push method (which is an alias of <<, the same
as concat) of collections, and there I want to test if :uniq is set to
true in the relationship. If so, the method should check if the passed
object is already related to the parent, or not (only then it will be
added).
But I just can''t find the original code of this method, so I could
overwrite
2006 Oct 31
5
conditional boost? friends to come up at top of search...
Hey guys, im trying to get my friends to come up at the top of the act
as ferret search. I would query the whole result set first, then move my
friends to the top, but the thing is, Im paginating my results and use
the offset and limit parameters in the multi_search() function.
Anyone know how to do this?
Thanks in advance...
--
Posted via http://www.ruby-forum.com/.
2006 May 22
3
STI, HABTM & counter_cache
Hello world...
I have an interesting issue that the online docs aren''t helping me with.
In my app I have 4 models
Item < ActiveRecord
Deal < Item
Product < Item
Category < ActiveRecord
Item has_and_belongs_to_many :categories...
On each category record I''d like to maintain a product_count and deal_count
to increase performance. This doesn''t appear
2006 Feb 27
4
2 belongs_to to the same parent table
Hello!
I have 2 table: users and buddies
User: id, name, ...
Buddy: id, user_id, user_buddy_id, ...
So if you have 2 users 1,jack and 2,fred and fred is a buddy of jack,
there is a Buddy object: id=1, user_id=1, user_buddy_id=2
I can declare only one belongs_to in Buddy and one has_many in User. And
there is conflict if I had the second one (the first one is discarded)
class User
has_many
2006 Jan 09
6
has_and_belongs_to_many :self
Hi all
I got the following class:
class Member < ActiveRecord::Base
has_and_belongs_to_many :buddies,
:class_name => ''Member'',
:join_table => ''members_have_buddies'',
:foreign_key => ''member_id'',
:association_foreign_key =>
2007 Nov 20
5
Compound search / grouping
Hi,
Following problem:
We have a tree structure with children and a root element (recursivly)
stored in one table (imagine a threaded forum).
Each of the children has a title which should be indexed by ferret.
Now we want to make a search that returns only the root and searches all
items.
So if one node has "expensive" and nother node has "car" I want to enter
2010 Sep 01
0
Error on update_attributes with HABTM relationship
Hope someone can tell me what''s up, because this has me baffled.
I have an Image model, and a Provider model. Both are defined as
"has_and_belongs_to_many" relating to each other. In my image form
partial, I have a bit of AJAX generating the provider_ids array
dynamically from a provider AJAX search on the same page.
So for example, each time I click on a provider name that
2008 May 12
3
Enumeration sum
Any idees why I can do this:
@sum = @selected.inject(0) { |sum, player| sum + player.value }
but not this:
@sum = @selected.sum { |player| player.value }
@sum = @selected.sum(&:value)
The last two give me following error:
wrong number of arguments (1 for 2)
/Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/associations/association_collection.rb:164:in
`calculate''
2006 Jan 23
0
type mismatch because of misled array index
Most times I look up a list of books I want to know a page reference for
each one, so the views that display booklists expect an array of
two-element arrays, constructed via a method in the model like so:
(class Category)
def book
@book = Array.new
self.bookshelves.each { |x| @book << [x.book, x.pages] }
return @book
end
When I get the list of all books that a user has
2006 Apr 05
4
validation on before_add callback
problem: validation error is not functionning in callback
callback for many-to-many association:
# keyword.rb
:before_add => :reject_self_related
def reject_self_related(related_keyword)
if (related_keyword == self)
# this does not work:
errors.add(:name, "Relating a keyword to itself is
forbidden!")
raise "Relating a keyword to itself is
2010 Oct 11
8
Nooby Stuck - "has_and_belongs_to_many" relationship
trying to set up a "has_and_belongs_to_many" relationship
would very much appreciate the help, not sure what im doing wrong at all.
Scheme.rb
class Scheme < ActiveRecord::Base
validates :schemename, :presence => true
belongs_to :user
has_many :levels, :dependent => :destroy
has_and_belongs_to_many :works
end
Work.rb
class Work < ActiveRecord::Base
2005 Dec 21
2
ActiveRecord Error with << in Collection
Hi
I get a following error:
ActiveRecord::AssociationTypeMismatch in Cmdb#create
ConfigurationItemAttributeValue expected, got Array
RAILS_ROOT: ./script/../config/..
Application Trace | Framework Trace | Full Trace
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:128:in
`raise_on_type_mismatch''
2007 Jun 01
0
"Exception: stack level too deep" on collection.clear?
Without getting too deep into my app I''m wondering if anyone has any
clues what might give rise to the error posted below. It occurs when
I run my unit tests.
class Actor < AR:Base
belongs_to :court_case
end
class CaseCause < AR:Base
belongs_to :court_case
end
class CourtCase < AR:Base
has_many :actors
has_many :case_causes
...
end
def my_test_method
...
2006 Apr 04
1
habtm and options_from_collection_for_select [1.0.0]
I ran into a problem using options_from_collection_for_select :
ActiveRecord::AssociationTypeMismatch (Topic expected, got String):
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/
active_record/associations/association_collection.rb:128:in
`raise_on_type_mismatch''
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/
2006 Jul 20
4
Help with Rails and postgres with sequence numbers (global?)
Hello,
I am building an Rails applications that has to integrate an legacy
system (lxoffice) (schema at:
http://www.lx-office.org/uploads/media/DB_Schema_2.1.1_R_393_17_03_2005_.pdf).
They somehow use global ids (global for some tables). I can not change
the schema and still I want to use Rails to access it and not pure SQL.
When I try to insert a record I get the following message:
2010 Jul 14
1
ActiveRecord::Observer, update_all and has_many
Hi,
I''m using an ActiveRecord::Observer to log users changing data in my
application. However, I''ve noted that adding an item to a has_many
relationship gets logged, but removing the item does not. That is:
@product.users << user
is logged, but:
@product.users.delete(user)
is not.
I''ve dug into ActiveRecord and found that the underlying issue is that
the
2006 Feb 27
1
Nested hash from form params into create()?
Hi,
I''m really excited about getting the create() method working on a
nested hash. The Rails book make it seem like I''m close to doing the
right thing but I think something very small is not quite right. The
error at the below isn''t giving any hints that I understand.
I have a form that produces a nested params hash and sends it to my
add_to_cart controller method.