Displaying 20 results from an estimated 40000 matches similar to: "after_save and associations"
2012 Jan 28
2
after_save in plugin
Dear list,
I am trying to override an after_save callback declared in main app
using plugin.
The main model class is:
class WorkHours < ActiveRecord::Base
@after_save_call_back_called=0
after_save :after_save_call_back
def after_save_call_back
logger.debug "after save called"
@after_save_call_back_called=1
end
end
And in my plugin (in the lib directory) :
module
2006 Jun 13
0
question about saving associations when using has_many :through
I''ve got a User model and a Book model. A user is required to submit
books each month to be reviewed by other users. I have the following
relationships defined:
# the join model
book_review.rb
belongs_to :user
belongs_to :book
end
book.rb
# each book is reviewed by many users. This allows us to get the BookReview
# objects associated with this book
has_many :book_reviews,
2006 Jan 06
6
HABTM problem not saving all associations
Hello all,
I have an Order object that has and belongs to many Products and has
and belongs to many Loan Types. This is so I can select multiples of
each in my order entry screen via checkbox groups.
I''m having some trouble with saving multiple HABTM associations for a
single model object; only the first HABTM association declared in the
model will save during the initial @order.save
2006 Feb 26
1
noob question on deleting from join tables
When I have a habtm association between two models (User & List), implemented
thru a join table users_lists, there are a variety of ways to "delete"
associations between specific User<->List combinations.
ActiveRecord lists delete, clear etc.
However, these methods all operate by setting foreign keys in the join table
to nil and do not remove rows from the join table.
2009 Mar 29
2
after_create and after_save
Hi,
AFAIK after_create is called after_save if the entry does not exist in
the DB.
Is there a way to tell if a specific model is new or not in
after_save?
I have to do something like:
def after_save
add_to_history ''save''
end
def after_create
add_to_history ''create''
end
However after_save is called when creating too.
--
cheers,
M.
2006 Jun 14
3
Prevent duplicate HABTM associations
Hi,
I am trying to restrict duplicate has_and_belongs_to_many associations.
I tried to find support for a association validation for duplicates but
couldn''t find anything.
I understand I could use :uniq=>true on the model association but this
would only prevent it from displaying duplicates, I want to stop
duplicate associations being inserted in the first place.
I have a vague
2006 May 12
2
How to declare several HABTM-relationships?
Hello,
Is it possible to have several HABTM-relationships for the same object?
Example: Imagine I have 3 objects (classroom, student, teacher) and
define the associations for the student as:
class Student < ActiveRecord::Base
has_and_belongs_to_many :classrooms
has_and_belongs_to_many :teachers
class Classroom < ActiveRecord::Base
has_and_belongs_to_many :students
class Teacher <
2010 May 12
2
How to add an after_save callback on runtime on a particular instance
class User < AR
end
user = User.new
Let''s say that I have a user instance with me. After this record is
saved I want an after_save callback which would print the id of the
record.
The only catch is that I am not allowed to change the User class. How
can I accomplish my goal? Is that even possible to add an after_save
callback on a particular instance object. Any metaprogramming
2006 Jul 17
2
Very strange after_save problem. Please help.
I have a very strange problem here. I do not get this. So any help is
greatly appreciated.
Basically I have a model that calls a method in the background
process in the "after_save" method. Let''s call the model products. So
what happens is this:
1. I create a new product.
2. Everything works and the product is saved with id 13.
3. A method is called in my background
2008 Dec 16
1
Callback when objects connect as a habtm relationship
If Product and Category models are in a habtm relationship, i.e.
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
end
I want a piece of code to be executed every time a product is connected to a
category.
Where do I put this code? Which callback (and on which model) will be
triggered?
Any
2008 Jul 28
1
callback executed like after_save but after transaction
I''m trying to add some code for my ActiveRecord class so that it is
executed whenever an object is updated, this code is a seperate
process that reads from the same table represented by my model class
so when it runs it needs the database to be up to date.
In the following situation this is fine:
# MyModel
def after_save
`/usr/local/bin/update_models -i #{self.id}`
end
# controller
2009 May 28
1
:has_many and :after_save
Having one to many connection (for example post has many comments), what
is the most efficient way of saving all comments when post is saved.
Using after_save on Post would introduce the n+1 problem. Is there any
way of saving the whole structure at once?
--
Posted via http://www.ruby-forum.com/.
2006 Jul 17
2
after_save deletes is an exception is raised?
Does after_save in a model delete if an exception is raised in there?
I simply did
def after_save
raise ''hmmm''
end
and it raised the exception but there was nothing in the database. I
removed that entire method and its in the database. Is this correct?
Thank You,
Ben Johnson
E: bjohnson@contuitive.com
-------------- next part --------------
An HTML attachment was
2008 Jun 13
1
Polymorphic :through associations
Could someone please help me with this association scheme? I am
familiar with habtm as well as the :through association but this one
has me stumped.
Here is an example DB.
Tags Table:
-------------------------
id
name
Taggables Table:
--------------------------
id
tag_id
taggble_type
taggable_id
For purposes of this example say that there are 2 other tables that
can be tagged and their model
2005 Jul 26
6
eager associations and limit/offset
Hi,
currently the eager association loading via ":include" does not work
when a has-many or habtm association is combined with offset/limit
(which is required for paginations). This is because in these cases, the
result set will contain more than one row per object, but limit/offset
works on row numbers.
I''ve developed a patch that makes this work by using 2 SQL statements
2006 Apr 17
0
Pros/cons of doubling up in Self-Referential has_many via :through
Relative newbie so would welcome comments re structure of a
self-referential relationship I''ve got.
It''s started off similar to the Person HABTM friends in the Rails
recipes books (working fine), but I needed to turn the join table into a
full-blown model as I wanted to add attributes to it. Few tricky bits
dealing with the new :through structure and understanding how :scope
2010 Aug 18
11
When after_save isn't after the save....
I have a Production model, which represents a farmer''s crop. After
create (via after_save) I call a method that creates one or more
Supply models, which represent a week of anticipated product harvest.
After each of those are created, a class method gets called that tries
to match each new Supply with outstanding orders for the same time
period. If a match is made, a transaction (an
2006 Jun 01
0
Disabling an after_save() in a rake conversion?
Hi all -
I''ve got a model Foo that has an after_save() method. I''d like to disable
it while doing a rake conversion.
Seems I should be able to just redeclare Foo.after_save() somehow, but I''m
not quite sure how to do that in the rake task...
Any ideas?
Thanks!
-philip
2005 Jul 28
0
ActiveRecord, computed values, and associations
Is there some sort of good pattern for dealing with computed values with
Rails/ActiveRecord?
A simple example: 3 tables, classes, students, and student_class_records
(habtm style relation but contains a good deal of additional state so is
modeled explicitly)
Something like select the 10 classes with the most students. Easy enough
in SQL. So I''m using a lot of find_by_sql and
2006 Aug 16
1
Naming rights_roles join model using has_many :through and polymorphic associations
Hi.
I have a couple of best practices questions regarding polymorphic
associations, naming join tables and user permissions.
Currently I have implemented the user authentication model from the
rails recipes book. Basically it goes something like this:
MODEL CLASSES:
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
end
class Role < ActiveRecord::Base