[prev in list] [next in list] [prev in thread] [next in thread] 

List:       markus-dev
Subject:    Re: [MarkUs-dev] Through associations
From:       "Daniel St. Jules" <danielst.jules () gmail ! com>
Date:       2013-03-24 21:56:13
Message-ID: 514F767D.3000909 () gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


I lied. :( I much prefer the simplicity of adding the one method to the 
model, as you originally suggested, because then I can treat both to_xml 
and to_json the same. Using procs and merge...I end up with separate 
logic for both formats, and it ends up looking pretty messy. It works, 
but it seems way too complex for such a simple addition to the output.

By defining the extra method in grouping.rb, all I need to do is:

             respond_to do |format|
               group_name = fields.include?(:group_name) ? [:group_name] 
: []
               format.any{render :xml => grouping.to_xml(:methods => 
group_name,
                 :only => fields, :root => 'groups', :skip_types => 'true',
                 :include => :students)}
               format.json{render :json => grouping.to_json(:methods => 
:group_name,
                 :only => fields, :include => :students)}
             end

Just one extra line for the respond_to block :) Hopefully no one will 
object to adding one little method to the model. The only other 
alternative I can think of would be to define a new to_xml within the 
model specifically for the API, but that seems even less practical.

Regards,

Daniel St. Jules
Platform Developer at ConnectInPrivate.com
www.danielstjules.com

On 13-03-24 4:58 PM, Daniel St. Jules wrote:
> Thanks again for the reply! I was hoping to avoid having to include 
> group, as the output would resemble something along the lines of:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <groupings>
>   <assignment-id>1</assignment-id>
>   <id>15</id>
>   <group>
>     <group-name>c6janace</group-name>
>   </group>
> </groupings>
>
> As you pointed out, we're then stuck with the extra level of XML, and 
> more complex JSON. I'd like to hide implementation details such as the 
> difference between groups and groupings as models - they're details 
> that the user doesn't know about, and preferably shouldn't have to. 
> It's a design decision I had been mauling over - sorry I didn't 
> mention it in my explanation.
>
> And thanks for pointing out that I can conditionally assign the 
> :include value like that... - I'm clearly overtired lol
>
> So, I had been leaning towards using your suggestion of the extra 
> method definition in groupings.rb, but I also just realized you can 
> pass procs to to_xml :) to_json doesn't have a builder, so I'd have to 
> handle it differently, but I should be able to simply use something 
> like grouping.as_json.merge(:group_name => grouping.group[:group_name])
>
> Though slightly more complex of a solution, it doesn't introduce a new 
> method to the model (which, as you pointed out, isn't ideal) and I'm 
> thinking I could use this to introduce some additional attributes to 
> the API (rel="self", or for discovering the different activities of a 
> resource)
>
> Thanks for the help!
> Daniel St. Jules
> Platform Developer at ConnectInPrivate.com
> www.danielstjules.com
> On 13-03-24 3:39 PM, Hanson Wu wrote:
>> I see. I agree that getting the ideal format for to_json (and/or 
>> to_xml) can sometimes be tricky!
>>
>> Anyhow, I'm not sure if either a new association, method, or any 
>> additional code in the model class is really the right solution.
>>
>> Have you looked at using :include inside the to_json call? Hows this:
>>
>> collection.to_json(only: [attr1, attr2 so on ...], include: 
>> [grouping: {only: :group_name}])
>>
>> If you want to exclude the group_name based on some condition, just 
>> add a line like so (or have a helper function if you have other 
>> filtering criteria as well):
>>
>> included = include_group_name? [grouping: {only: :group_name}] : []
>> then modify: collection.to_json(only: [attr1, attr2 so on ...], 
>> include: included)
>>
>> In the view, just be sure to account for the extra level for group 
>> name (ie in xml it will be <group><group_name>Name</group_name></group>).
>>
>> Let me know if this helps!
>>
>> Good luck,
>> Hanson
>>
>> On Sun, Mar 24, 2013 at 11:39 AM, Daniel St. Jules 
>> <danielst.jules@gmail.com <mailto:danielst.jules@gmail.com>> wrote:
>>
>>     Hi Hanson,
>>
>>     Thanks for the reply! I'm working on the Markus API, and response
>>     output is handled using to_xml
>>     (http://apidock.com/rails/ActiveRecord/Serialization/to_xml) and
>>     to_json. They'll print out the attributes of a model, which is
>>     why I hoped to define group_name as one.
>>
>>     Using your method would work in that I can do the following to
>>     have it included:
>>     collection.to_xml(:methods => :grouping_name, ...)
>>
>>     The issue then is how I handle filtering out which attributes are
>>     displayed for a collection/resource, as a user may want to limit
>>     what information is returned. I use ":only" to determine which
>>     attributes are returned. Unfortunately, :methods seems to be
>>     evaluated and included after :only, meaning the grouping_name
>>     attribute would be included even if it's supposed to be ignored.
>>     A dirty solution would mean duplicating the respond_to block I
>>     have and handling the condition separately for when they want to
>>     have grouping_name included, and when they don't.
>>
>>     So it would work, but I'm curious as to whether or not we can
>>     define it using an association or something, as it would end up
>>     being cleaner :)
>>
>>     I don't think :through will work as it's meant to provide an
>>     association with another model, not just an attribute.
>>     accepts_nested_attributes is almost what I want, except you still
>>     have to access it through a method (modelname_attributes = { :key
>>     => val }), ie: grouping.group_attributes[:group_name], which is
>>     just about the same thing as grouping.group[:group_name] in the
>>     first place lol
>>
>>     Any other ideas? I appreciate the help!
>>
>>     Thanks!
>>     Daniel
>>
>>     Daniel St. Jules
>>     Platform Developer at ConnectInPrivate.com
>>     www.danielstjules.com  <http://www.danielstjules.com>
>>
>>     On 13-03-24 3:03 AM, Hanson Wu wrote:
>>>     Hi Daniel,
>>>
>>>     Just happened to see this (I'm a student from a couple terms
>>>     ago). What are you trying to achieve where you can't go the
>>>     normal grouping_instance.group.group_name route?
>>>
>>>     Anyhow, not sure what you mean by needing direct access, but
>>>     this would do the trick? Of course it's not ideal, hence why I'm
>>>     curious what you are trying to do :)
>>>
>>>       def grouping_name
>>>         return self.group.group_name
>>>       end
>>>
>>>     Cheers,
>>>     Hanson
>>>
>>>     On Sat, Mar 23, 2013 at 6:54 PM, Daniel St. Jules
>>>     <danielst.jules@gmail.com <mailto:danielst.jules@gmail.com>> wrote:
>>>
>>>         Hi everyone,
>>>
>>>         I'd like to add ":group_name" to grouping.rb for use with
>>>         the Markus API. Given that each grouping belongs to a group,
>>>         I thought I'd use the same value assigned to that.
>>>
>>>         I thought I could use a through association, like this, in
>>>         grouping.rb:
>>>         has_one :group_name, :through => :group
>>>
>>>         But so far no luck. Any help or suggestions would be greatly
>>>         appreciated! I know I could access this value by doing
>>>         grouping_instance.group.group_name, but for what I'm trying
>>>         to achieve, I need direct access to that name attribute from
>>>         grouping.
>>>
>>>         Thanks,
>>>         Daniel
>>>
>>>         -- 
>>>         Daniel St. Jules
>>>         Platform Developer at ConnectInPrivate.com
>>>         www.danielstjules.com <http://www.danielstjules.com>
>>>
>>>         _______________________________________________
>>>         Markus-dev mailing list
>>>         Markus-dev@markusproject.org
>>>         <mailto:Markus-dev@markusproject.org>
>>>         http://markusproject.org/cgi-bin/mailman/listinfo/markus-dev
>>>
>>>
>>
>>
>


[Attachment #5 (text/html)]

<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">I lied. :( I much prefer the simplicity
      of adding the one method to the model, as you originally
      suggested, because then I can treat both to_xml and to_json the
      same. Using procs and merge...I end up with separate logic for
      both formats, and it ends up looking pretty messy. It works, but
      it seems way too complex for such a simple addition to the output.<br>
      <br>
      By defining the extra method in grouping.rb, all I need to do is:<br>
      <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; respond_to \
                do |format|<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
group_name = fields.include?(:group_name) ?  [:group_name] : []<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
format.any{render :xml =&gt;  grouping.to_xml(:methods =&gt; group_name, <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
:only =&gt; fields, :root =&gt; 'groups',  :skip_types =&gt; 'true', <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
                :include =&gt; :students)}<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
format.json{render :json =&gt;  grouping.to_json(:methods =&gt; :group_name,<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
                :only =&gt; fields, :include =&gt; :students)}<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<br>
      <br>
      Just one extra line for the respond_to block :) Hopefully no one
      will object to adding one little method to the model. The only
      other alternative I can think of would be to define a new to_xml
      within the model specifically for the API, but that seems even
      less practical.<br>
      <br>
      Regards,<br>
      <pre class="moz-signature" cols="72">Daniel St. Jules
Platform Developer at ConnectInPrivate.com
<a class="moz-txt-link-abbreviated" \
href="http://www.danielstjules.com">www.danielstjules.com</a></pre>  On 13-03-24 4:58 \
PM, Daniel St. Jules wrote:<br>  </div>
    <blockquote cite="mid:514F68FE.6080706@gmail.com" type="cite">
      <meta content="text/html; charset=ISO-8859-1"
        http-equiv="Content-Type">
      <div class="moz-cite-prefix">Thanks again for the reply! I was
        hoping to avoid having to include group, as the output would
        resemble something along the lines of:<br>
        <br>
        &lt;?xml version="1.0" encoding="UTF-8"?&gt;<br>
        &lt;groupings&gt;<br>
        &nbsp; &lt;assignment-id&gt;1&lt;/assignment-id&gt;<br>
        &nbsp; &lt;id&gt;15&lt;/id&gt;<br>
        &nbsp; &lt;group&gt;<br>
        &nbsp;&nbsp;&nbsp; &lt;group-name&gt;c6janace&lt;/group-name&gt;<br>
        &nbsp; &lt;/group&gt;<br>
        &lt;/groupings&gt;<br>
        <br>
        As you pointed out, we're then stuck with the extra level of
        XML, and more complex JSON. I'd like to hide implementation
        details such as the difference between groups and groupings as
        models - they're details that the user doesn't know about, and
        preferably shouldn't have to. It's a design decision I had been
        mauling over - sorry I didn't mention it in my explanation.<br>
        <br>
        And thanks for pointing out that I can conditionally assign the
        :include value like that... - I'm clearly overtired lol <br>
        <br>
        So, I had been leaning towards using your suggestion of the
        extra method definition in groupings.rb, but I also just
        realized you can pass procs to to_xml :) to_json doesn't have a
        builder, so I'd have to handle it differently, but I should be
        able to simply use something like
        grouping.as_json.merge(:group_name =&gt;
        grouping.group[:group_name])<br>
        <br>
        Though slightly more complex of a solution, it doesn't introduce
        a new method to the model (which, as you pointed out, isn't
        ideal) and I'm thinking I could use this to introduce some
        additional attributes to the API (rel="self", or for discovering
        the different activities of a resource)<br>
        <br>
        Thanks for the help!<br>
        <pre class="moz-signature" cols="72">Daniel St. Jules
Platform Developer at ConnectInPrivate.com
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" \
href="http://www.danielstjules.com">www.danielstjules.com</a></pre>  On 13-03-24 3:39 \
PM, Hanson Wu wrote:<br>  </div>
      <blockquote
cite="mid:CAFZWfg=zMZuXvYD_FNY5FjEoTXSdQ662qQaVdZSn4aPAjFz4MQ@mail.gmail.com"
        type="cite">I see. I agree that getting the ideal format for
        to_json (and/or to_xml) can sometimes be tricky!
        <div><br>
        </div>
        <div>Anyhow, I'm not sure if either a new association, method,
          or any additional code in the model class is really the right
          solution.&nbsp;</div>
        <div><br>
        </div>
        <div>Have you looked at using :include inside the to_json call?
          Hows this:</div>
        <div><br>
        </div>
        <div>collection.to_json(only: [attr1, attr2 so on ...], include:
          [grouping: {only: :group_name}])<br>
          <br>
          If you want to exclude the group_name based on some condition,
          just add a line like so (or have a helper function if you have
          other filtering criteria as well):<br>
          <br>
          included = include_group_name? [grouping: {only: :group_name}]
          : []</div>
        <div>then modify:&nbsp;collection.to_json(only: [attr1, attr2 so on
          ...], include: included)</div>
        <div><br>
        </div>
        <div>In the view, just be sure to account for the extra level
          for group name (ie in xml it will be
          &lt;group&gt;&lt;group_name&gt;Name&lt;/group_name&gt;&lt;/group&gt;).</div>
  <div><br>
        </div>
        <div>Let me know if this helps!<br>
          <br>
          Good luck,</div>
        <div>Hanson<br>
          <br>
          <div class="gmail_quote">On Sun, Mar 24, 2013 at 11:39 AM,
            Daniel St. Jules <span dir="ltr">&lt;<a
                moz-do-not-send="true"
                href="mailto:danielst.jules@gmail.com" \
target="_blank">danielst.jules@gmail.com</a>&gt;</span>  wrote:<br>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              <div bgcolor="#FFFFFF" text="#000000">
                <div>Hi Hanson,<br>
                  <br>
                  Thanks for the reply! I'm working on the Markus API,
                  and response output is handled using to_xml (<a
                    moz-do-not-send="true"
                    href="http://apidock.com/rails/ActiveRecord/Serialization/to_xml"
                    target="_blank">http://apidock.com/rails/ActiveRecord/Serialization/to_xml</a>)
  and to_json. They'll print out the attributes of a
                  model, which is why I hoped to define group_name as
                  one.<br>
                  <br>
                  Using your method would work in that I can do the
                  following to have it included:<br>
                  collection.to_xml(:methods =&gt; :grouping_name, ...)<br>
                  <br>
                  The issue then is how I handle filtering out which
                  attributes are displayed for a collection/resource, as
                  a user may want to limit what information is returned.
                  I use ":only" to determine which attributes are
                  returned. Unfortunately, :methods seems to be
                  evaluated and included after :only, meaning the
                  grouping_name attribute would be included even if it's
                  supposed to be ignored. A dirty solution would mean
                  duplicating the respond_to block I have and handling
                  the condition separately for when they want to have
                  grouping_name included, and when they don't. <br>
                  <br>
                  So it would work, but I'm curious as to whether or not
                  we can define it using an association or something, as
                  it would end up being cleaner :)<br>
                  <br>
                  I don't think :through will work as it's meant to
                  provide an association with another model, not just an
                  attribute. accepts_nested_attributes is almost what I
                  want, except you still have to access it through a
                  method (modelname_attributes = { :key =&gt; val }),
                  ie: grouping.group_attributes[:group_name], which is
                  just about the same thing as
                  grouping.group[:group_name] in the first place lol<br>
                  <br>
                  Any other ideas? I appreciate the help!<br>
                  <br>
                  Thanks!<span class="HOEnZb"><font color="#888888"><br>
                      Daniel</font></span>
                  <div class="im"><br>
                    <pre cols="72">Daniel St. Jules
Platform Developer at ConnectInPrivate.com
<a moz-do-not-send="true" href="http://www.danielstjules.com" \
target="_blank">www.danielstjules.com</a></pre>  </div>
                  <div>
                    <div class="h5"> On 13-03-24 3:03 AM, Hanson Wu
                      wrote:<br>
                    </div>
                  </div>
                </div>
                <div>
                  <div class="h5">
                    <blockquote type="cite">Hi Daniel,
                      <div><br>
                      </div>
                      <div>Just happened to see this (I'm a student from
                        a couple terms ago). What are you trying to
                        achieve where you can't go the normal
                        grouping_instance.group.group_name route?</div>
                      <div> <br>
                      </div>
                      <div>Anyhow, not sure what you mean by needing
                        direct access, but this would do the trick? Of
                        course it's not ideal, hence why I'm curious
                        what you are trying to do :)</div>
                      <div><br>
                      </div>
                      <div>
                        <div>&nbsp; def grouping_name</div>
                        <div>&nbsp; &nbsp; return self.group.group_name</div>
                        <div>&nbsp; end</div>
                        <div><br>
                        </div>
                        <div>Cheers,</div>
                        <div>Hanson</div>
                        <br>
                        <div class="gmail_quote">On Sat, Mar 23, 2013 at
                          6:54 PM, Daniel St. Jules <span dir="ltr">&lt;<a
                              moz-do-not-send="true"
                              href="mailto:danielst.jules@gmail.com"
                              target="_blank">danielst.jules@gmail.com</a>&gt;</span>
                          wrote:<br>
                          <blockquote class="gmail_quote"
                            style="margin:0 0 0 .8ex;border-left:1px
                            #ccc solid;padding-left:1ex">Hi everyone,<br>
                            <br>
                            I'd like to add ":group_name" to grouping.rb
                            for use with the Markus API. Given that each
                            grouping belongs to a group, I thought I'd
                            use the same value assigned to that.<br>
                            <br>
                            I thought I could use a through association,
                            like this, in grouping.rb:<br>
                            has_one :group_name, :through =&gt; :group<br>
                            <br>
                            But so far no luck. Any help or suggestions
                            would be greatly appreciated! I know I could
                            access this value by doing
                            grouping_instance.group.group_name, but for
                            what I'm trying to achieve, I need direct
                            access to that name attribute from grouping.<br>
                            <br>
                            Thanks,<br>
                            Daniel<span><font color="#888888"><br>
                                <br>
                                -- <br>
                                Daniel St. Jules<br>
                                Platform Developer at
                                ConnectInPrivate.com<br>
                                <a moz-do-not-send="true"
                                  href="http://www.danielstjules.com"
                                  target="_blank">www.danielstjules.com</a><br>
                                <br>
_______________________________________________<br>
                                Markus-dev mailing list<br>
                                <a moz-do-not-send="true"
                                  href="mailto:Markus-dev@markusproject.org"
                                  \
target="_blank">Markus-dev@markusproject.org</a><br>  <a moz-do-not-send="true"
                                  \
                href="http://markusproject.org/cgi-bin/mailman/listinfo/markus-dev"
                                  \
target="_blank">http://markusproject.org/cgi-bin/mailman/listinfo/markus-dev</a><br>  \
</font></span></blockquote>  </div>
                        <br>
                      </div>
                    </blockquote>
                    <br>
                  </div>
                </div>
              </div>
            </blockquote>
          </div>
          <br>
        </div>
      </blockquote>
      <br>
    </blockquote>
    <br>
  </body>
</html>



_______________________________________________
Markus-dev mailing list
Markus-dev@markusproject.org
http://markusproject.org/cgi-bin/mailman/listinfo/markus-dev


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic