Ruby on Rails
Sunday, July 5, 2015
Padmahas,
Do you understand the notion of specifying associations between tables in Rails? Let's me know and I'll try to explain? In general and in anticipation of your answer do you understand INNER JOINS in SQL?
Liz
On Sunday, July 5, 2015 at 11:46:46 AM UTC-4, Padmahas Bn wrote:
-- Do you understand the notion of specifying associations between tables in Rails? Let's me know and I'll try to explain? In general and in anticipation of your answer do you understand INNER JOINS in SQL?
Liz
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 youOn 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") Sorry, should be
>
>
> #<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.
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/63da3f7c-3451-4b2d-aa2e-8e4f362f06b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment