View not working with . but works with [' ']

I have some weird view behavior going around which I am unable to understand.

<% for b in @admin_budget %>    <td><%= b['rental'] %></td>    <td><%= b['phone'] %></td>    <td><%= b['consumables'] %></td> <% end %>

works... but

<% for b in @admin_budget %>    <td><%= b.rental %></td>    <td><%= b.phone %></td>    <td><%= b.consumables %></td> <% end %>

does not work.

b.inspect throws up #"7", "current_approval_status"=>"N", "misc"=>"1000", "place_id"=>nil, "updated_at"=>"2007-03-11", "approved_by"=>nil, "created_by"=>"3", "total"=>"0", "updated_by"=>nil, "id"=>"3", "phone"=>"1500", "year"=>"2007", "salaries"=>"5000", "rental"=>"2500", "approved_at"=>nil, "consumables"=>"1000", "created_at"=>"2007-03-11"}>

and b.class throws up AdminBudget

What is happening here? Any clues?

Regards, Rajesh

Quick addition...

Just before the b.rental I am using b.month and b.year and b.user.name. They produce the expected results! Wits end, folks.

Regards, Rajesh

K. Rajesh wrote:

I have some weird view behavior going around which I am unable to understand.

<% for b in @admin_budget %>    <td><%= b['rental'] %></td>    <td><%= b['phone'] %></td>    <td><%= b['consumables'] %></td> <% end %>

works... but

<% for b in @admin_budget %>    <td><%= b.rental %></td>    <td><%= b.phone %></td>    <td><%= b.consumables %></td> <% end %>

does not work.

b.inspect throws up #"7", "current_approval_status"=>"N", "misc"=>"1000", "place_id"=>nil, "updated_at"=>"2007-03-11", "approved_by"=>nil, "created_by"=>"3", "total"=>"0", "updated_by"=>nil, "id"=>"3", "phone"=>"1500", "year"=>"2007", "salaries"=>"5000", "rental"=>"2500", "approved_at"=>nil, "consumables"=>"1000", "created_at"=>"2007-03-11"}>

and b.class throws up AdminBudget

What is happening here? Any clues?

Regards, Rajesh

If you check b.methods what do you see?

Few more questions: - What is b.class.superclass? I assume ActiveRecord::Base? - Does AdminBudget override rental, phone or consumables with its own accessors/methods? If so, you should check into these to make sure they're working correctly. - What does b.respond_to?('rental') say? It should return true...

Thats a fairly big list... here goes

["logger", "table_name_prefix=", "instance_values", "update_attributes", "save_with_validation", "toggle!", "after_save", "build_user", "singleton_methods", "logger=", "clear_aggregation_cache", "hash", "send", "enable_warnings", "initialize", "record_timestamps", "save!", "update", "attributes_before_type_cast", "instance_of?", "before_validation_on_update", "nil?", "record_timestamps=", "attribute_names", "valid?", "remove_subclasses_of", "instance_exec", "id_before_type_cast", "before_create", "allow_concurrency", "create_user", "protected_methods", "clear_association_cache", "silence_stderr", "tainted?", "create_with_callbacks", "instance_eval", "table_name_suffix", "user", "allow_concurrency=", "untaint", "kind_of?", "update_attribute", "after_validation_on_update", "equal?", "save_without_validation!", "display", "table_name_suffix=", "user=", "blank?", "load", "errors", "after_create", "destroy", "private_methods", "b64encode", "silence_stream", "update_without_callbacks", "readonly!", "set_place_target", "is_a?", "freeze", "attribute_present?", "before_destroy", "increment", "gem", "create", "save_with_validation!", "eql?", "require", "has_user?", "before_update", "subclasses_of", "returning", "quoted_id", "update_attribute_without_validation_skipping", "id", "method", "generate_read_methods", "public_methods", "lock_optimistically", "extended_by", "`", "id=", "attributes", "update_with_callbacks", "generate_read_methods=", "pluralize_table_names", "build_place", "has_attribute?", "after_destroy", "extend", "attributes=", "initialize_with_callbacks", "lock_optimistically=", "configurations", "pluralize_table_names=", "user?", "type", "after_update", "with_options", "update_attribute_with_validation_skipping", "object_id", "configurations=", "instance_variables", "suppress", "copy_instance_variables_from", "increment!", "frozen?", "destroy_without_callbacks", "destroy_without_transactions", "dclone", "create_place", "to_a", "decrement", "validate_on_create", "create_without_timestamps", "place", "update_with_lock", "class", "validate", "before_validation", "place=", "schema_format", "instance_variable_get", "destroy_with_callbacks", "encode64", "destroy_with_transactions", "verification_timeout", "to_param", "colorize_logging", "schema_format=", "to_s", "validate_on_update", "respond_to_without_attributes?", "new_record?", "create_or_update_with_callbacks", "create_with_timestamps", "verification_timeout=", "primary_key_prefix_type", "colorize_logging=", "reload", "clone", "after_validation", "to_json", "to_yaml", "instance_variable_set", "primary_key_prefix_type=", "has_place?", "==", "require_library_or_gem", "===", "decrement!", "save_without_transactions", "to_yaml_properties", "column_for_attribute", "__id__", "to_xml", "locking_enabled?", "to_yaml_style", "inspect", "toggle", "decode_b", "require_gem", "valid_without_callbacks", "connection", "update_without_timestamps", "readonly?", "silence_warnings", "respond_to?", "before_validation_on_create", "create_or_update", "dup", "=~", "place?", "before_save", "decode64", "save_with_transactions", "extend_with_included_modules_from", "save", "save_without_validation", "__send__", "set_user_target", "taguri", "default_timezone", "daemonize", "", "transaction", "methods", "=", "update_with_timestamps", "valid_with_callbacks", "taguri=", "table_name_prefix", "default_timezone=", "taint", "after_validation_on_create"]

Regards, Rajesh

Mmmm... I had an attr_reader specified for each of those things... when I took it out, things started working again with the . operator. Why does this happen?

Rajesh

Hi,

Quick addition...

Just before the b.rental I am using b.month and b.year and b.user.name. They produce the expected results! Wits end, folks.   

since rental is not a reserved word either of ruby or AR , I would suggest that you do a project-wide (including plugins) of "def rental" or "def self.rental" or even better if your search tool allows regular expresions "def .* rental.*". That should show you where you are overwriting the rental method.

If you are not overwriting it, and assuming your @admin_budget object holds an array of AR objects, then it makes no sense to me, unless you have been playing with AR method missing definition.

regards,

javier ramirez

Because ActiveRecord implements accessors in method_missing. If you actually define a method or accessor with the same name as a column in your model, ActiveRecord won't be able to handle it for you.

Is there a reason you need to specify an attr_reader for those columns?

No, actually. No reason. I removed it and it works. I was just wondering why it didnt work. Learning is (from eden li)

"If you actually define a method or accessor with the same name as a column in your model, ActiveRecord won't be able to handle it for you"

Cool,.

Rajesh