Ruby on Rails Sunday, July 5, 2015

 #<Role::ActiveRecord_Relation:0x00000005f4f5d8>  is not an error or junk value, it's actually information.  Informing you that you have created an ActiveRecord::Relation  instance of class Role.

When this object is returned, try to run to iterate through it (for xxx in object ... content.. end).  You'll see that it has database content for Role.

Read more about ActiveRecord at: https://github.com/rails/rails/tree/master/activerecord



On Sunday, July 5, 2015 at 11:46:46 AM UTC-4, Padmahas Bn wrote:
Even though it sounds technically right, I don't know why rails showing some junk values for some of the query you suggested and some of the query I tried.

Again there is no exception but this is the result: #<Role::ActiveRecord_Relation:0x00000005f4f5d8>

I think as you said I have to specify association between tables so that it would be easy to query without headache.

Thank you

On Sun, Jul 5, 2015 at 9:04 PM, Colin Law <cla...@gmail.com> wrote:
On 5 July 2015 at 16:07, Padmahas Bn <padm...@gmail.com> wrote:
> Hello Colin these are the results I got
>>
>> >Try
>> >Role.where(id: user.roleid).select("role_name")
>
>
> #<Role:0x000000061d80e8>
>
> An invalid value. As I stated in my question this is similar to <% rolename
> = Role.select("role_name").where(id: user.roleid) %> but "where" and
> "select" are inter changed. So same invalid value.

Sorry, should be
Role.where(id: user.roleid).select(:role_name)

Swapping the order of method calls completely changes the meaning.
Role.select().where() fails because Role.select returns an array, and
one cannot call where on an array.  Role.where() returns an
ActiveRecord::Relation, and it is valid to call select on that.

>
>
>>
>> >Or even better
>> >user.role.select("role_name")
>
>
> private method `select' called for nil:NilClass
> Wouldn't work in my case because, I think it requires an Association between
> user and role table. And there Must be a field on both tables named
> "role_name". Which doesn't exist

Not role_name, but there there should be a relation between the two.
If you have a field in user which maps to the id of a role then
conventionally this would be called role_id and you would specify
User belongs_to role
Role has_many users
Then you could do as I have suggested.

I think you could benefit from working right through a good tutorial
such as railstutorial.org (which is free to use online) which would
show you the basics of rails.

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/CAL%3D0gLtE%2BCCD3RMvnZZWpaYMGnOXEeZz6g7_ePvm8UKXWxqBCA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7d719991-6b93-4ed3-88c4-48ad43a7f730%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment