nested-attributes

Ruby on rails: fields_for do nothing if defined submodel_attributes=

I have such code in new.erb.html: <% form_for(@ratification) do |f| %> <%= f.error_messages %> <% f.fields_for :user do |fhr| %> <p> <%= fhr.label :url %><br /> <%= fhr.text_field_with_auto_complete :url %> </p> <% end %> <% end %> If i have empty Ratification.rb it is ok, fields_for works ok. But if I wr...

Whats the fasted way to extract an array of nested objects from an array of objects in Ruby ?>

I have an array of Elements, and each element has a property :image. I would like an array of :images, so whats the quickest and least expensive way to achieve this. Is it just iteration over the array and push each element into a new array, something like this: images = [] elements.each {|element| images << element.image} ...

Has_and_belongs_to_many accepts_nested_attributes_for problem

I am working on some code today but for some reason I can't seem to get it to work. Some explanation: I have a system that contains feeds, that consist of feed_entries. There are also some categories with sub_categories. Between the sub_categories and feed_entries there is a has_and_belongs_to_many relationship, which I can not seem to ...

Solving AttributeErrors in nested attributes

I am writing a small mocking class to do some tests. But this class needs to support the idea of having nested attributes. This example should provide some insight to the problem: class Foo(object): def __init__(self): self.x = True From the above class, we can have: f = Foo() f.x I know I can add attributes falling ...

SQL Subqueries COUNT CASE

I'd like a SELECT query to return specific value if the count of a nested subquery is 0 ... SELECT ( SELECT (CASE COUNT(*) = 0 THEN 'TRUE' ELSE 'FALSE' END) FROM List WHERE Status = 1 AND Deleted = 1 ) AS Status This does not work what is wrong with this syntax? ...

RoR: fields_for and partials.

hi, i am using nested_attributes for an association of products back to a transaction. I need to add a date to the products model. I would like to add a link to set the date_select to today. here is the view: <% f.fields_for :trans_products do |builder| %> <%= render 'trans_product_fields', :f => builder %> ...

Pseudo-dicts as properties

I have a Python class C which should have two pseudo-dicts a and b. The term pseudo-dicts means that the dictionaries don't actually exist and that they are “recomputed” each time a key is accessed. In pseudocode this would look like this: class C: def a.__getitem__(self, key): return 'a' def b.__getitem__(self, key): ...

rails: create Parent, if doesn't exist, whilte creating child record

Any best practices for the following?: I have Manufacturer model that has_many Inventory In my new Inventory form I want a field that maps to Manufacturer.name so that when one submits the new Inventory form the app: searches for a manufacturer with the 'name' from the form if it exists then assign the id to @inventory.manufacturer...

Rails Nested Model Attribute 'accepts_nested_attributes_for' possible bug on create

I expect found a bug in rails v2.3.8, but I'm asking here in case I'm just doing something stupid. I have a 3-deep nested model that describes a shipment (i.e. shipment -> boxes -> line-items in boxes). The shipment and boxes are created at the same time, but the line-items already exist, from when the order was placed, so line-items j...

How do I use multiple "one-to-many" nested attributes in form_for

given the fact that a user has many credit cards and a credit card has many addresses, I am trying to create a form that creates a user and credit card with address all at once relavent model code: class User < ActiveRecord::Base has_many :credit_cards accepts_nested_attributes_for :credit_cards end class CreditCard < ActiveRecord...

Possible to eager load associations with nested_attributes?

Just briefly, I have run into a dreaded 2(n) queries problem. If n = the number of skills in the database, then my characters#edit form will take 2(n) queries to load the page. It will SELECT a PlayerSkill (the join table) once every skill, and it will look up the Skill once per skill. Here is some code which I believe is pertinent to t...

multiple paperclip attachements with watermark processor

Hi guys! I'm having a ton of trouble with uploading multiple attachments with paperclip and processing them with a watermark. I have 2 models, Ad and Photo. The Ad has_many :photos and the Photo belongs_to :ad. To be more exact in /models/ad.rb I have: class Ad < ActiveRecord::Base has_many :photos, :dependent => :destroy accep...

Modifying attributes on the join model with accepts_nested_attributes_for

Simply, a Contact can have various associated Time Windows, which may or may not be Active as a Schedule. To wit: Models class Contact < ActiveRecord::Base has_many :schedules has_many :time_windows, :through => :schedules accepts_nested_attributes_for :schedules, :allow_destroy => true end class TimeWindow < ActiveRecord::Base ...

Rails belongs_to and single table inheritance not behaving

I have a Bike model and a Component model. Several models inherit from Component: Frame, Chain, Crankset etc. When I submit my form, my params look like this: "bike" => { "frame" => { "id" => "4" }, "chain" => { "id" => "19" }, ... } In my controller, the following code breaks: @bike = Bike.new(params[:bike]) > Frame(#90986230) expe...

having trouble passing formbuilder using remote_function with nested_attributes_for

Hey all I am having trouble updating a partial that contains a nested_attributes_for form with AJAX. My application needs to load a new section of the form based on the event type chosen in the main form. The fields associated with each type are stored in a database table. At the moment, when I select an event type, the id of the even...

accepts_nested_attributes_for and new records

Hi, I'm using accepts_nested_attributes_for with the following models: User model: class User < ActiveRecord::Base has_many :competences has_many :skills, :through => :competences, :foreign_key => :skill_id accepts_nested_attributes_for :skills end Skill model: class Skill < ActiveRecord::Base has_many :competences has_m...

How do I set a filter that will reject existing records when using accepts_nested_attributes_for

I have a Message and Source model related as follows: class Message < ActiveRecord::Base has_many :sources accepts_nested_attributes_for :sources, :allow_destroy => true, :reject_if => proc{|s| s[:href].blank?} end class Source < ActiveRecord::Base belongs_to :outgoing_message validates_presence_of :href end When I submit my ...

Ignore validation failure when using accepts_nested_attributes_for (i.e. prevent rollback)?

So suppose I have the Person and Child models: class Person < ActiveRecord::Base has_many :children accepts_nested_attributes_for :children end class Child < ActiveRecord::Base belongs_to :parent, :class_name => "Person" validates_presence_of :name end Now when I use a nested form and save a Person with 2 new children...

accepts_nested_attributes_for with belongs_to polymorphic

Hi, I would like set up a polymorphic relation with accepts_nested_attributes_for. Here is the code: class Contact <ActiveRecord::Base has_many :jobs, :as=>:client end class Job <ActiveRecord::Base belongs_to :client, :polymorphic=>:true accepts_nested_attributes_for :client end When I try to access Job.create(..., :client_at...

accepts_nested_attributes_for in combination with formtastic and paperclip

Hi guys I am trying I have a simple one-to-many association. I am trying to update photos that belong to an album through a nested form: The edit form for the photo album: <%= semantic_form_for(@album, :url => user_album_path(@user, @album), :html => {:method => :put} ) do |f| %> <%= f.inputs do %> <%= f.input :title %> <%= f.in...