Re: [Rails] Condition on fields_for

77 views
Skip to first unread message

Walter Lee Davis

unread,
May 21, 2015, 6:00:45 PM5/21/15
to rubyonra...@googlegroups.com
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

On May 21, 2015, at 3:36 PM, Elizabeth McGurty <emcg...@gmail.com> wrote:

> 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
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ae5620b7-a237-4ba2-ac52-1596fa3b7d8a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Stewart Mckinney

unread,
May 21, 2015, 6:14:45 PM5/21/15
to rubyonra...@googlegroups.com
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.

Stewart Mckinney

unread,
May 21, 2015, 6:15:25 PM5/21/15
to rubyonra...@googlegroups.com
*changes to one association will not be reflected in the other until reload.
Message has been deleted
Message has been deleted
Message has been deleted

Colin Law

unread,
May 22, 2015, 5:05:25 PM5/22/15
to rubyonra...@googlegroups.com
On 22 May 2015 at 20:28, Elizabeth McGurty <emcg...@gmail.com> wrote:
> 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..

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
Message has been deleted
Message has been deleted

Colin Law

unread,
May 24, 2015, 3:59:58 AM5/24/15
to rubyonra...@googlegroups.com
On 24 May 2015 at 02:56, Elizabeth McGurty <emcg...@gmail.com> 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.

> title varchar(50) NOT NULL,
> category_id int(3) NOT NULL DEFAULT '99',
> ...
> class Advertiser < ActiveRecord::Base
>
> require 'uri'
>
> self.primary_key = 'advertiser_id'
> attr_accessor :item_image_upload
> has_one :item_image
> 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.

> ...
> 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

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
Message has been deleted

Colin Law

unread,
May 24, 2015, 3:25:06 PM5/24/15
to rubyonra...@googlegroups.com
On 24 May 2015 at 19:10, Elizabeth McGurty <emcg...@gmail.com> 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
Message has been deleted

Colin Law

unread,
May 24, 2015, 5:00:50 PM5/24/15
to rubyonra...@googlegroups.com
On 24 May 2015 at 21:48, Elizabeth McGurty <emcg...@gmail.com> 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
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/95f53a50-7ca4-4d7d-9cf5-54c1ec09079e%40googlegroups.com.
Message has been deleted

Colin Law

unread,
May 25, 2015, 4:26:47 PM5/25/15
to rubyonra...@googlegroups.com
On 24 May 2015 at 22:22, Elizabeth McGurty <emcg...@gmail.com> 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
Message has been deleted
Message has been deleted
Message has been deleted

botp

unread,
Jun 3, 2015, 2:44:32 AM6/3/15
to rubyonra...@googlegroups.com
On Tue, May 26, 2015 at 5:56 AM, Elizabeth McGurty <emcg...@gmail.com> 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
Reply all
Reply to author
Forward
0 new messages