Displaying 20 results from an estimated 1000 matches similar to: "Strange behaviour with ActiveRecord has_many collections, Rails 4 compared to Rails 3"
2006 Apr 05
3
CRUD pattern for has_many relationships (forms containing collections)?
Hello folks!
Beare with me for a second, while I explain my problem.
Assuming we have the trivial model of
class Author
has_many :books
end
class Book
end
How do people go about creating the authors/edit and authors/new
views if you want to be able to add and remove arbitrary amount of
Books at Author creation and edit time? What I mean by this is that I
go to
2013 Jul 21
4
how to use activemodel collection.build for a has_many :through association
Hi all,
In my controller I am doing the following to populate a nested form for a
has_many through association:
def new
@specification = Specification.new
Component.find_each.each do |component|
@specification.component_specifications.build(:component_id =>
component.id)
end
The idea being whenever someone creates or edits a form, it will be
populated with all
2006 Aug 11
1
Avoiding adding duplicates to has_many collection?
Since there''s no ''exists?'' method for a has_many collection, is there an
easier way to avoid adding duplicates to a collection? (I assume
obj.items << item would add duplicates if repeatedly called.) Right now
I do:
category = Category.find_or_create_by_name(category_name)
directory.categories << [ category ] if
directory.categories.find(:first,
2006 Mar 13
1
has_many.collection<<obj doesn''t set attributes?
I tried:
@item.stuff<<thing
and in thing I have:
def stuff_id=(value)
# remember old stuff_id cuz Rails has lousy trigger equivalent
logger.debug ''set stuff_id called''
end
And apparently stuff_id doesn''t get called, but the fk is changed
directly in the database? I''m about to abandon trying to do trigger
related stuff in Rails and go back to
2005 May 24
2
Deleting from a 'has_many' collection doesn't set the foreign key to NULL
Hi all,
I have an Active Record problem. Having this classes declaration.
class Sale < ActiveRecord::Base
has_many :sale_line_items, :class_name ''SaleLineItem''
...
end
class SaleLineItem < ActiveRecord::Base
...
end
When I try to delete an item in a Sale class method from sale_line_items
(sale_line_items.delete(li)), it extracts ''li'' from
2011 Aug 17
1
has_many :through, collection<<(object) and duplicates.
Hi,
I''m facing a strange behavior in Rails with the has_many association and
the generated collection<<(object, ...) method that comes along.
I''ve got an Object model and a Tag model, plus a taggings table to join
the two together.
On Object, I set these associations:
has_many :taggings, :as => :taggable, :dependent => :destroy
has_many :tags,
2005 Aug 03
4
R CMD build error
Dear list,
I try to update the prabclus package.
R CMD check works nicely, no warnings, good results in all tests.
However, building the package fails:
ginkgo:/disk5/home/chrish/RAusw/libsrc R CMD build prabclus
* checking for file 'prabclus/DESCRIPTION' ... OK
* preparing 'prabclus':
* checking whether 'INDEX' is up-to-date ... OK
* removing junk files
* building
2014 Apr 02
0
Trying to understand eager loading and accessing collections from within instance methods
I'm trying to optimize some of my queries by taking advantage of eager
loading.
I've created a gist with the specific problem I'm seeing
here: https://gist.github.com/tybenz/9944205
Any ideas why the eager loading works for grabbing the collection from the
rails console but a SQL query is required when I try to access the
collection from inside the instance method?
Is there
2014 Apr 06
0
collection_check_boxes with has_many through: association Rails 4
I am trying to list all the categories a user can be interested in with
check boxes so that a user can submit these interests through my join table.
collection_radio_buttons works with a simple has_many, belongs_to
association:
<%= collection_radio_buttons(:listing, :category_id, Category.all, :id,
:name do |b| %>
<%= b.label { b.radio_button} %>
<%= b.text %>
2014 Apr 11
0
Replacement for "has_many ... counter_sql" in Rails 4.1
Hi
I'm looking for ways to replace "has_many ... counter_sql: '...'" in order
to migrate an app from Rails 3.2 to 4.0 (done) and then up to 4.1 which has
dropped support for "counter_sql". An example is on Stackoverflow:
http://stackoverflow.com/questions/22988321/replacement-for-has-many-counter-sql-in-rails-4-1
I'd do it with a separate counter method,
2013 Sep 17
4
Rails 4 deprecation of in-place edit methods for collection associations
Hi everyone!
For those of you who are not noticed the in-place edit method for
collection associations are broken in the current rails release - 4.0.0,
and here''s what is cooking in the rails master
https://github.com/rails/rails/commit/1a40be02113287d9a003f8224e91b9f623857f4b
and this https://github.com/rails/rails/pull/12227
In short: it looks like things like has_many.reject!{},
2006 Aug 12
7
Collection assignment to a has_many :through
I''m working on a simple photo gallery in rails, it seems to be a good
project for a newbie.
I have photos and categories, many-to-many association. It worked well
with HABTM. Then I decided that it would be good to be able to change
order of the photos so that thumbnail pages would look less chaotic.
So I created a Layout model which is a join model (or whatever it is
called) that
2006 Apr 20
3
has_many :through with has_many/has_many join models
It seems that using a join model that joins with two has_many''s will
fail to generate proper SQL
class StudentSemesterRecord < ActiveRecord::Base
belongs_to :semester
has_many :discipline_records, :through => :semester
end
class Semester < ActiveRecord::Base
has_many :student_semester_records
has_many :discipline_records
end
class DisciplineRecord <
2006 Jul 06
0
Using in_place_editor_field for collections?
I am trying to use in_place_editor_field to make many objects editable
at the same time. I think I solved the first step of this problem;
before it was giving me a problem about referencing a nil, so I did
this:
<%= in_place_editor_field ''question[]'', :name %>
adding the [] seemed to fix things. Now all the fields show up on the
page and they are all clickable. But
2006 Jul 19
0
Clearing dependent collections
I have a user model that owns messages :
class User < ActiveRecord::Base
has_many :messages , :dependent => :nullify
end
I''m using nullify so that when the user is deleted, the messages
continue to exist - they''re just marked as being ownerless.
However, calling user.messages.clear actually deletes all his
messages, which I was a little surprised at. I would
2006 Jul 20
0
Getting joined collections on a form
Greetings, I''m looking for suggestions on how to simplify getting customer
addresses on a form:
I''m jumping between controller and view several times; alternating between
rhtml and rjs, in order to display customers and their addresses on an
invoicing form. It''s very messy.
I have a one to many relationship between Customer and Addresses, each
customer has one or more
2006 Jun 04
2
Manipulating collections
I appreciate this is basic stuff but can''t seem to find the answer
anywhere using Google. If someone could point me in the direction of
some documentation on this I would be very appreciative.
If I have a collection as follows:
@stuff = Stuff.find_by_type(''new'')
And I then want to add something onto it, how can I add to this
collection?
Additionally how could I go
2006 Jun 12
0
Realtime form validation for forms containing collections
Hi,
I''m successfully using the realtime_form_validation plugin from
http://railsrtv.rubyforge.org/ However, my forms contain collections,
as described in the AWDWR book. Basically my form looks like this.
2004 2005 2006
Question 1 12 13 15
Question 2 9 11 13
I have the following code to generate ids for my input fields.
2006 Jun 17
0
Dynamic forms containing collections
Hiall,
I have the following problem: I need to use forms containing
collections (i will call them FCC), and I want to construct them
dynamically, as I need to deal with quite large forms (more than 600
input fields).
The variables @model_name and @field_name contain the proper model and
field names as strings, which is enough for "normal" form input
fields. (i.e. the "if
2006 Apr 12
0
Validating collections
I saw this issue addressed in mid-January under the
heading ''TIP: Using field_error_proc to add style
attributes to form elements'', but I didn''t quite get
it.
I have a collection of objects that I want to input in
one form and validate (during update). The suggestion
in the post I referred to above was to use something
like
[in form]
> <% for @comment in