小编Nom*_*Nom的帖子

如何正确选择连接表中的字段?

这是我的简化模型,我加入了模型,

样板房

   has_many :towers
Run Code Online (Sandbox Code Playgroud)

模型塔

   belong_to :apartment
Run Code Online (Sandbox Code Playgroud)

然后我试图加入控制器中的两个表也尝试在rails控制台,如下所示:

Apartment.joins(:塔).select('apartments.id','apartments.name','towers.id','towers.name')

问题是上面的查询只返回

apartments.id和apartments.name

也尝试使用这样的别名,仍然没有运气

Apartment.joins(:塔).select('apartments.id','apartments.name','towers.id as towerid','towers.name as towername')

我已经确认所有塔楼都有公寓,我知道我可以做到这一点来获得1条记录

Apartment.joins(:塔).select('apartments.id','apartments.name','towers.id','towers.name').first.towers.id

等等,但我需要所有记录和所有这些字段,请咨询.

这是我在rails控制台中获得的最新结果:

 Apt Load (1.0ms)  SELECT apts.id, apts.apt_name, towers.id as towerid, towers.
    tower_name as towername FROM `apts` INNER JOIN `towers` ON `towers`.`apt_id` = `
    apts`.`id`
    => #<ActiveRecord::Relation [#<Apt id: 5, apt_name: "basura">, #<Apt id: 5, apt_
    name: "basura">, #<Apt id: 124, apt_name: "hydra">, #<Apt id: 124, apt_name: "hy
    dra">, #<Apt id: 126, apt_name: "mediterania">, #<Apt id: 126, apt_name: "mediterania">, #<Apt …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails ruby-on-rails-4 rails-activerecord

9
推荐指数
3
解决办法
1万
查看次数

rails nested form always "POST" even on edit mode? caused route error

i work on an application which have 1 module with many to many relationship, its working fine, i can create my "unit" with nested form, but i got 1 problem when i am on edit state, when i hit submit button it throw me an error.

No route matches [POST] "/units/27"
Run Code Online (Sandbox Code Playgroud)

i thought the action should be patch instead of post?? i inspect the generated html in browsers, and i can see there is a hidden field named "_method" with …

ruby ruby-on-rails nested-forms nested-attributes ruby-on-rails-4

7
推荐指数
1
解决办法
294
查看次数

如何从aspx代码中的泛型主体中获取自定义数据?

我正在构建一个应用程序并将其与活动目录集成.

因此,我将我的应用程序用户验证为活动目录用户,之后我将一些用户数据存储为:user group and user profile将通用主体和用户身份存储到通用身份.

我的问题是,当我想使用它时,我无法从通用主体获取用户配置文件数据.

有人可以告诉我该怎么做吗?

 string cookieName = FormsAuthentication.FormsCookieName;
 HttpCookie authCookie = Context.Request.Cookies[cookieName];

 if (authCookie == null)
 {
       //There is no authentication cookie.
       return;
 }

 FormsAuthenticationTicket authTicket = null;

 try
 {
       authTicket = FormsAuthentication.Decrypt(authCookie.Value);
 }
 catch (Exception ex)
 {
      //Write the exception to the Event Log.
       return;
 }

 if (authTicket == null)
 {
      //Cookie failed to decrypt.
      return;
 }

 String data = authTicket.UserData.Substring(0, authTicket.UserData.Length -1);
 string[] userProfileData =   data.Split(new char[] { '|' });
 //Create an …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net-4.5

4
推荐指数
1
解决办法
6506
查看次数