Condition on fields_for

I have a parent table. And associated with that parent table is two
possible addresses, a primary one and/or an alternative: (has_many and
accepts_nested_attributes_for).

Rather than creating a second addresses table, I have used a flag,
address_type to distinguish primary from alternative, which will
apparently
only be useful if I can create a condition on fields_for

But now as I explore and research more, seems that I cannot create a
condition on fields_for addresses, type => ‘alternative’ or ‘primary’.

I want to utilize Rails associations… Should I just create tables:
primary_addresses and alternative_addresses?

Thanks,

Liz

Do these addresses have any difference besides being primary or
secondary? Are there extra fields in one that are not in the other? Or
is primary maybe a boolean on the address object?

Walter

*changes to one association will not be reflected in the other until
reload.

On Thu, May 21, 2015 at 6:14 PM, Stewart M. [email protected]

Create two associations with conditions on the parent table with
conditions
that point to each “type” of address.

For instance:

has_many :addresses

has_one :primary_address,
->() do
where( :type => :primary )
end,
:class_name => Address

This will make primary address accessible through addresses but also
make
it its own association ( so you can just pass that association to the
form
).

Be aware that changes to one association will not be reflected in the
other. So if you change “primary_address”, before you reload, the object
in
addresses which corresponds to your primary address will not have the
changes you made to primary address. After you save/reload, they will.

On Thu, May 21, 2015 at 6:00 PM, Walter Lee D. [email protected]

THANKS! Sorry I responded to Walter before I read this

Thanks, but I think that I figured it out

In parent AR,

attr_accessible :addresses_attributes
has_many :addresses ### Pretty sure should be plural for has_many, and
singular for has_one
accepts_nested_attributes_for :addresses

Then in the view I have (abbreviated)

form @parent_object do |parent_form| ### In the controller I just
pass
@parent_object = Parent_object.new (AR takes care of all the
associations)
… the address sections

parent_form.fields_for :addresses do |primary|
primary.text_field :address_line_1
primary.hidden_field :address_type_id, :value => ‘primary’
… end

parent_form.fields_for :addresses do |alternative|
alternative.text_field :address_line_1
alternative.hidden_field :address_type_id, :value => ‘alternative’
… end

I think this may work. Haven’t looked at the params yet…

Walter, Stewart… Many thanks to you. Definitely making so much
progress
here.

All seems to be going really well, but I remain stumped at a critical
point

Given the model, I pass from the controller @parent = Parent.object

All of the field_for aspects are working really beautifully. But in the
view I need to test for a parent/child value. I know that this code is
wrong:

<% unless @lenders.state_id_string.blank? %> ## @lenders is the parent
model, and only the fields within that model are being returned. I
thought
that if I define all parent associations in the AR, then the object
@lenders would be aware…
##
I need to learn whether the parent with address value has
state_id_string
blank?
##
I’m stuck. How do I do this?
<% display_state_id_string = ‘inline’ %>
<% else %>
<% display_state_id_string = ‘none’ %>
<% end %>

Thanks,

Liz

Thank you very, very much!

a =
Lender.find(‘7oV71d1oVtxfyS9Ra6hnUtNe31N’).addresses.select(‘state_id_string’)

a.blank?

On 22 May 2015 at 20:28, Elizabeth McGurty [email protected] wrote:

<% unless @lenders.state_id_string.blank? %> ## @lenders is the parent
model, and only the fields within that model are being returned. I thought
that if I define all parent associations in the AR, then the object @lenders
would be aware…

Is @lenders a single parent or an array? Presumable an array since it
is plural. In which case you can’t just test state_id_string.
Possibly you need something like @lenders.where(some condition on
state_id_string), assuming that state_id_string is a database field.
Alternatively perhaps you want to use @lenders.each to select and test
them one at a time.

Colin

On 24 May 2015 at 02:56, Elizabeth McGurty [email protected] wrote:

Okay… I am really trying to be super vigilant to best Ruby/Ruby on Rails
practices here… the whole n + 1 matter particularly

I have a parent table called Advertiser:

This is how it looks on the database:

CREATE TABLE advertisers (
advertiser_id varchar(40) NOT NULL,

If you are committed to the rails conventions then that should just be
id, not advertiser_id. advertiser_id should be used in another table
that belongs_to advertiser.

has_one :category
You have has_one category, but also a category_id in the table either
it should be belongs_to category or the field should not be there.

class Category < ActiveRecord::Base

self.primary_key = ‘category_id’

belongs_to :advertiser
belongs_to :borrower
belongs_to :lender

You need _id fields for each of the above.


I am really committed to using best practices. What an I doing wrong?
I am not clear as to model requirement regarding look up values that are
user nonchangeable… The whole notion, for example, has_one with regard to
a look up is not clear to me.

Have you worked right through a good tutorial such as
railstutorial.org (which is free to use online), including all the
exercises? That should help to make the basics of rails more clear.

Colin

Okay… I am really trying to be super vigilant to best Ruby/Ruby on
Rails
practices here… the whole n + 1 matter particularly

I have a parent table called Advertiser:

This is how it looks on the database:

CREATE TABLE advertisers (
advertiser_id varchar(40) NOT NULL,
title varchar(50) NOT NULL,
category_id int(3) NOT NULL DEFAULT ‘99’,
category_other varchar(50) DEFAULT NULL,
description varchar(50) DEFAULT NULL,
advertiser_url varchar(50) NOT NULL,
advertiser_email varchar(50) NOT NULL,
approved int(3) NOT NULL,
date_created datetime NOT NULL,
date_updated datetime DEFAULT NULL,
date_deleted datetime DEFAULT NULL,
is_active varchar(3) NOT NULL,
is_activated int(3) DEFAULT NULL,
remote_ip varchar(50) DEFAULT NULL,
PRIMARY KEY (advertiser_id),
KEY name (title),
KEY date_created (date_created)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

This is its model: It is listed as advertiser.rb

class Advertiser < ActiveRecord::Base

require ‘uri’

self.primary_key = ‘advertiser_id’
attr_accessor :item_image_upload
has_one :item_image
has_one :category

accepts_nested_attributes_for :item_image
before_create :get_advertiser_primary_key_value
before_create :url_valid

validates_uniqueness_of :title,:if => :title, :case_sensitive => true,
:message => " already exists."
validates_uniqueness_of :advertiser_email,:if => :advertiser_email,
:case_sensitive => false, :message => " already exists"
validates_uniqueness_of :advertiser_url,:if => :advertiser_url,
:case_sensitive => false, :message => " already exists"

def url_valid
uri = URI.parse(self.advertiser_url)
if uri.kind_of?(URI::HTTP)
return true
else
self.errors.add(:advertiser_url, “: URL Link improperly formatted:
http://”)
return false
end

end

protected

def get_advertiser_primary_key_value
self.advertiser_id = get_random
end

def get_random
length = 36
characters = (‘A’…‘Z’).to_a + (‘a’…‘z’).to_a + (‘0’…‘9’).to_a
@id = SecureRandom.random_bytes(length).each_char.map do |char|
characters[(char.ord % characters.length)]
end.join
@id
end

end

And the parent table has a look up table source called categories:

Here is how it looks on the database:

CREATE TABLE categories (
category_id int(3) NOT NULL DEFAULT ‘99’,
category_type varchar(50) NOT NULL,
KEY category_type_index (category_type)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Categories exists as category.rb, and this is the model:

class Category < ActiveRecord::Base

self.primary_key = ‘category_id’

belongs_to :advertiser
belongs_to :borrower
belongs_to :lender

end

Here is the Advertiser controller:

class AdvertiserController < ApplicationController
def new
@advertiser = Advertiser.new

respond_to do |f|
  f.html
end


end

end

And now here is the new.html.erb view with irrelevant stuff excluded…

<%= form_for @advertiser, :url=> {:controller=>“advertiser”, :action =>
“create”},:html=> {:class=>“advertiser”, :multipart=>true} do |nf| %>

<% unless @advertiser.errors.empty? %>
<%= render :partial => ‘home/check_for_errors’, :locals => {:data_source
=>
@advertiser} %>
<% end %>

Advertisement Category:
THIS IS WHERE I AM HAVING Problems…
Tried fields_for
Every variety of Rails select
<%= nf.select :category_id, Category.all.collect {|p| [
p.category_type, p.category_id ] } %> ### This doesn’t work

I am really committed to using best practices.  What an I doing 

wrong?
I am not clear as to model requirement regarding look up values that are
user nonchangeable… The whole notion, for example, has_one with regard
to
a look up is not clear to me.

<% end %>

Thanks, Liz McGurty

On 24 May 2015 at 19:10, Elizabeth McGurty [email protected] wrote:

Thank you so much! Very helpful. I have to get over my old-school
practices.

Who are you saying thank you to? The quoted message did not seem to
include any help. It might be better to put your reply after the
relevant section of the message you are replying to (as I have done
here), then it is easier to follow what you are saying.

Colin

Thank you so much! Very helpful. I have to get over my old-school
practices.
Liz

On 24 May 2015 at 21:48, Elizabeth McGurty [email protected] wrote:

I am thanking you, Colin

That is ok, glad to be of help. However I note you ignored my other
suggestion, or at least did not act on it. I was:

It might be better to put your reply after the
relevant section of the message you are replying to (as I have done
here), then it is easier to follow what you are saying.

Cheers

Colin

Colin you are kind of scaring me. Maybe I selected the wrong "Reply’
icon,
but I think that I have always thanked folks. You have no idea how much
I
appreciate what I have learned. My plan was that when my app was
settled,
incorporating all I learn I was going to give a big Shout Out!

I am thanking you, Colin

What suggestion did I ignore?

On 24 May 2015 at 22:22, Elizabeth McGurty [email protected] wrote:

Colin you are kind of scaring me. Maybe I selected the wrong "Reply’ icon,
but I think that I have always thanked folks.

Nobody has suggested that you have not thanked folks. What I was
commenting on was the fact that you have been replying at the top of
the previous message rather than replying to it with comments inserted
in the previous message at appropriate points, which I think is more
appropriate for technical mailing lists and is generally the
convention on this list. I see that you are using gmail, perhaps you
do not know that gmail hides the previous message by default, but if
you click on the three little dots that appear at the bottom of the
text window when you click Reply then it will show the previous
message so you can insert your reply at the appropriate point.

Cheers

Colin

Colin,

I pretty much would prefer if you didn’t bother with my posts. I am
functioning in earnest, and think that folks understand that. I don’t
want
you policing my site conduct.

Liz

On Tue, May 26, 2015 at 5:56 AM, Elizabeth McGurty [email protected]
wrote:

Colin,
I pretty much would prefer if you didn’t bother with my posts. I am
functioning in earnest, and think that folks understand that. I don’t want
you policing my site conduct.
Liz

This is an old post, but I hope I can help with the seeming confusion :
)
Liz, what Colin was referring to is this
http://en.wikipedia.org/wiki/Posting_style.
Colin prefers inline-or-bottom-posting-with-trimmed-quotes over
top-posting-with-full-quotes (wc you prefer). In a way, Colin is
telling you to help him help you better.

hope that helps.
kind regards --botp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs