Ruby on Rails
Sunday, July 5, 2015
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 <clanlaw@gmail.com> wrote:
On 5 July 2015 at 16:07, Padmahas Bn <padmahas@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-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/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/CAC_h78JOEJyD5tewQfhbictLQC4Te%3DyXaRYtvOXTLMiSjM4bmg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment