Dynamic fields -- accessor methods are not really dynamic?

674 views
Skip to first unread message

Vojto Rinik

unread,
Apr 5, 2010, 2:47:31 AM4/5/10
to mon...@googlegroups.com
Hello,

I was wondering why there aren't really dynamic accessor methods:


I tried to create a new document and change an attribute that wasn't defined
anywhere before.

I'm trying to craft a truly dynamic CMS where documents (pages) each have
template and the attribute set can vary for different types of documents/pages.

So what is the solution

1. Don't use accessor methods, use write_attribute instead
2. Always use field class methods so that there would be accessors.

Thanks

Vojto Rinik
http://infinite.sk

“Perfection is achieved perfection not when
there is nothing left to add, but when there is 
nothing left to take away” 
– Antoine de Saint-Exupery

Durran Jordan

unread,
Apr 5, 2010, 10:49:19 PM4/5/10
to mon...@googlegroups.com, mon...@googlegroups.com
I see no reason not to go fully dynamic at this point as long as it's configurable... The initial thought was to retain some sort of usual behaviour with method missing but at this point I think we are all versed enough in mongodb to handle nils getting returned on random methods. I'll put a card in to update it.

- Email von das Handy.

Justin Smestad

unread,
Apr 6, 2010, 6:52:39 PM4/6/10
to mon...@googlegroups.com
+1 would love to see this added. I am running into the same.

Regards,
Justin Smestad
justin....@gmail.com

Kurt

unread,
Apr 6, 2010, 7:11:16 PM4/6/10
to Mongoid
I think I'd prefer that they don't, actually. I'd much rather use
index based access to get at all the dynamic things than have random
methods return nil. :)

This would let you get at Mongo document field names that don't work
as method names too.

-Kurt

On Apr 5, 9:49 pm, Durran Jordan <dur...@gmail.com> wrote:
> I see no reason not to go fully dynamic at this point as long as it's  
> configurable... The initial thought was to retain some sort of usual  
> behaviour with method missing but at this point I think we are all  
> versed enough in mongodb to handle nils getting returned on random  
> methods. I'll put a card in to update it.
>
> - Email von das Handy.
>
> On 05/04/2010, at 1:47, Vojto Rinik <vo...@rinik.net> wrote:
>
>
>
> > Hello,
>
> > I was wondering why there aren't really dynamic accessor methods:
>

> >http://github.com/durran/mongoid/blob/master/lib/mongoid/attributes.r...

Durran Jordan

unread,
Apr 6, 2010, 7:41:25 PM4/6/10
to mon...@googlegroups.com, Mongoid
That's pretty much why I said it should remain configurable - you
could set use_dynamic_fields to false and go through read/
write_attribute when necessary and not run into the issue described.
(btw this is going to be a model level config option in 2.0.0.beta3
this week.

- Email von das Handy.

> --
> To unsubscribe, reply using "remove me" as the subject.

Durran Jordan

unread,
Aug 6, 2010, 9:34:59 AM8/6/10
to Zilog, Mongoid
Well there were more ongoing discussions around this and the feeling
was that this could cause more confusion on the Ruby side and a
compromise was made... Take the case where a method is called, maybe a
setter in this case that was done in developer error. Instead of the
developer being alerted with a MethodMissingError, they would have an
attribute set with that method name and may not know something was
wrong for quite some time... This is why method_missing checks if an
attribute exists in the hash already before giving the dynamic getters
and setters.

write_attribute will work, and the second that attribute is there you
have a getter and a setter. I am willing to add the square brackets
accessors to alias read_attribute and write_attribute for a more
shorthand syntax - but I think it still may be too much to not check
on method_missing since it then messes with the contract that the
document model provides.

I'll repost this to the list for thoughts. I am still open for
discussion on what people think.

On 5 August 2010 23:23, Zilog <imp...@gmail.com> wrote:
> Hi, Just found this thread about dynamic attributes.
>
> It appears dynamic setters don't work even with allow_dynamic_fields
> active?
>
> Aok if that's the final decisionn since write_attribute works - just
> not was I was expecting from the conversation below.
>
> (rails3, edge mongoid)
>
> Let me know and many thanks for mongoid!
>
> -Z


>
>
> On Apr 6, 4:41 pm, Durran Jordan <dur...@gmail.com> wrote:
>> That's pretty much why I said it should remain configurable - you
>> could set use_dynamic_fields to false and go through read/
>> write_attribute when necessary and not run into the issue described.
>> (btw this is going to be a model level config option in 2.0.0.beta3
>> this week.
>>
>> - Email von das Handy.
>>
>> On 06/04/2010, at 18:11, Kurt <mrk...@gmail.com> wrote:
>>
>>
>>
>> > I think I'd prefer that they don't, actually.  I'd much rather use

>> > index based access to get at all thedynamicthings than have random


>> > methods return nil. :)
>>
>> > This would let you get at Mongo document field names that don't work
>> > as method names too.
>>
>> > -Kurt
>>
>> > On Apr 5, 9:49 pm, Durran Jordan <dur...@gmail.com> wrote:

>> >> I see no reason not to go fullydynamicat this point as long as it's


>> >> configurable... The initial thought was to retain some sort of usual
>> >> behaviour with method missing but at this point I think we are all
>> >> versed enough in mongodb to handle nils getting returned on random
>> >> methods. I'll put a card in to update it.
>>
>> >> - Email von das Handy.
>>
>> >> On 05/04/2010, at 1:47, Vojto Rinik <vo...@rinik.net> wrote:
>>
>> >>> Hello,
>>

>> >>> I was wondering why there aren't reallydynamicaccessor methods:


>>
>> >>>http://github.com/durran/mongoid/blob/master/lib/mongoid/attributes.r
>> >>> ...
>>
>> >>> I tried to create a new document and change an attribute that wasn't
>> >>> defined
>> >>> anywhere before.
>>

>> >>> I'm trying to craft a trulydynamicCMS where documents (pages) each

Shashank

unread,
Oct 3, 2010, 9:49:54 PM10/3/10
to Mongoid
Hi,

I am keen on knowing if there is any progress on this issue.

On Aug 6, 8:34 am, Durran Jordan <dur...@gmail.com> wrote:

> write_attribute will work, and the second that attribute is there you
> have a getter and a setter.

I am unable to get write_attribute to work for dynamic attributes.
Here is my code: http://www.pastie.org/1197623

I am on Mac OS (Snow Leopard) with the following configuration:

ruby 1.8.6 (2007-03-13 patchlevel 0) [universal-darwin8.0]
mongo db version v1.6.2
gems:
mongo (1.0.8)
mongoid (1.9.1)


> I'll repost this to the list for thoughts. I am still open for
> discussion on what people think.

Thanks,
- Shashank

Shashank

unread,
Oct 4, 2010, 6:46:40 PM10/4/10
to Mongoid
Hi,

Replying to my own post :)

On Oct 3, 8:49 pm, Shashank <std...@gmail.com> wrote:

> I am unable to get write_attribute to work for dynamic attributes.
> Here is my code:http://www.pastie.org/1197623
>
> I am on Mac OS (Snow Leopard) with the following configuration:
>
> ruby 1.8.6 (2007-03-13 patchlevel 0) [universal-darwin8.0]
> mongo db version v1.6.2
> gems:
> mongo (1.0.8)
> mongoid (1.9.1)
>

Got it to work with the following changes to the config:

$ ruby -v
ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.4.0]

$ gem list --local mongo

*** LOCAL GEMS ***

mongo (1.0.7)
mongoid (2.0.0.beta.17)

$
Reply all
Reply to author
Forward
0 new messages